Lodash early forEach vs. every

Benchmark created by John-David Dalton on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.2/lodash.min.js"></script>
<script src="http://dl.dropbox.com/u/513327/lodash.js"></script>
<script>
(function() {
  var forEach2 = _.noConflict().forEach;
  _.forEach2 = forEach2;
}());

_.forEach3 = (function() {
  var propertyIsEnumerable = {}.propertyIsEnumerable,
      nativeKeys = Object.keys;

  function createCallback(func, thisArg, accumulating) {
    if (!func) {
      return identity;
    }
    if (typeof func != 'function') {
      return function(object) {
        return object[func];
      };
    }
    if (typeof thisArg != 'undefined') {
      if (accumulating) {
        return function(accumulator, value, index, object) {
          return func.call(thisArg, accumulator, value, index, object);
        };
      }
      return function(value, index, object) {
        return func.call(thisArg, value, index, object);
      };
    }
    return func;
  }

  return function(collection, callback, thisArg) {
    var index, iteratee = collection, result = collection;
    if (!collection) return result;
    callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg);
    var length = iteratee.length; index = -1;
    if (typeof length == 'number') {  
      while (++index < length) {
        if (callback(iteratee[index], index, collection) === false)
          return result;
      }
    }
    else {    
      var skipProto = typeof iteratee == 'function' && 
        propertyIsEnumerable.call(iteratee, 'prototype');
    
      var ownIndex = -1,
          ownProps = objectTypes[typeof iteratee] ? nativeKeys(iteratee) : [],
          length = ownProps.length;

      while (++ownIndex < length) {
        index = ownProps[ownIndex];
        if (!(skipProto && index == 'prototype')) {
          if (callback(iteratee[index], index, collection) === false)
            return result;
        }
      }      
    }
    return result;
  };
}());
</script>

Setup

var _ = window._;
    var array = Array(101).join('x').split('');

Test runner

Ready to run.

Testing in
TestOps/sec
_.every
_.every(array, function(value, index) {
  return index != 50;
});
ready
_.forEach
_.forEach(array, function(value, index) {
  return index != 50;
});
ready
_.forEach2
_.forEach2(array, function(value, index) {
  return index != 50;
});
ready
_.forEach3
_.forEach3(array, function(value, index) {
  return index != 50;
});
ready

Revisions

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

  • Revision 1: published by John-David Dalton on