lodash vs. nativeEach (v47)

Revision 47 of this benchmark created by frogger on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>

Setup

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];
    };

Test runner

Ready to run.

Testing in
TestOps/sec
_.each(obj)
_.each(testObj, testCallback);
 
ready
nativeEach(obj)
nativeEach(testObj, testCallback);
 
ready
_.each(arr)
_.each(testArray, testCallback);
 
ready
nativeEach(arr)
nativeEach(testArray, testCallback);
 
ready

Revisions

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