Lodash forEach vs for

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var a = [];
    for (var i = 0; i < 1000; i++) {
      a[i] = i;
    }
   
    var ObjectOne = {};
    var ObjectTwo = {};
    var ObjectTree = {};
    var ObjectFour = {};
   
    var callbackOne = function(val, key) {
      ObjectOne[key] = val;
    }
    var callbackTwo = function(val, key) {
      ObjectTwo[key] = val;
    }
    var callbackThree = function(val, key) {
      ObjectTree[key] = val;
    }
    var callbackFour = function(val, key) {
      ObjectFour[key] = val;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
while
var l = a.length;
var i = -1;
while(++i<l){
    callbackOne(a[i],i);
}
 
pending…
for
var i;
for(i = 0; i < a.length; i++){
    callbackTwo(a[i],i);
}
 
pending…
forEach
a.forEach(callbackThree);
 
pending…
lodash forEach
_.forEach(a, callbackFour);
 
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment