lodash vs. nativeEach

JavaScript performance comparison

Revision 46 of this test case created by frogger

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    function isArray(obj) {
      if (Array.isArray) {
        return Array.isArray(obj);
      }
      return Object.prototype.toString.call(obj) === '[object Array]';
    }
   
    function nativeEach(obj, cb) {
      if (isArray(obj)) {
        eachArray(obj, cb);
      } else {
        eachObject(obj, cb);
      }
    }
   
    function eachArray(obj, cb) {
      for (var i = 0, i < obj.length; i++) {
        if (cb(obj[i], i, obj) === false) {
          break;
        }
      }
    }
   
    function eachObject(obj, cb) {
      for (var prop in obj) {
          if (cb(obj[prop], prop, obj) === false) {
            break;
          }
      }
    }
   
    var testArray = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }, { id: 7 }, { id: 8 }, { id: 9 }, { id: 10 }];
   
    var testObj = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10 };
   
    var buffer;
    var testCallback = function (v, k) {
      buffer = [v, k];
    };
   
};
</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
_.each(obj)
_.each(testObj, testCallback);
 
pending…
nativeEach(obj)
nativeEach(testObj, testCallback);
 
pending…
_.each(arr)
_.each(testArray, testCallback);
 
pending…
nativeEach(arr)
nativeEach(testArray, testCallback);
 
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

Comment form temporarily disabled.

Add a comment