jquery vs lodash vs. nativeEach (v50)

Revision 50 of this benchmark created by Justin Lau on


Description

Using jQuery v2.1.3 and lodash v3.0.0

Preparation HTML

<script src="//cdn.jsdelivr.net/lodash/3.0.0/lodash.compat.min.js"></script>
<script src="//code.jquery.com/jquery-2.1.3.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
$.each(obj)
$.each(testObj, testCallback);
ready
$.each(arr)
$.each(testArray, testCallback);
ready

Revisions

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