lodash vs. underscore vs. native js foreach loops

Benchmark created by jim on


Description

Compare native forEach, for loop, underscore and lodash .each.

Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<script>var lodash = _.noConflict();</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script>var underscore = _.noConflict();</script>

Setup

var arr = [];
    for (var i = 0; i < 10000; i++) {
      arr.push({
        Number: i
      });
    };

    var e = null;
    
    function doWork(o, i) {
      e = o;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
For loop using hash key
for (var key in arr) {
  e = arr[key];
};
ready
For loop using index
for (var i = 0, len = arr.length; i <= len; i++) {
  e = arr[i];
};
ready
Native forEach
arr.forEach(function(o, i) {
  e = o;
});
ready
Native forEach Named func
arr.forEach(doWork);
ready
Lodash forEach
lodash.forEach(arr, doWork);
ready
Underscore forEach
underscore.forEach(arr, doWork);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.