Underscore.each vs jQuery.each vs. for loop vs. nimble-foreach (v147)

Revision 147 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="//documentcloud.github.com/underscore/underscore-min.js">
</script>
<script src="https://raw.github.com/chrisevans/nimble/master/nimble.js">
</script>

<script>
var pi = Math.PI,
a = "t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t".split(","), e; 

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery.each
$.each(a, function(index, val) {
  val = pi;
});
ready
good old for loop
for (var i = 0, len = a.length; i < len; i++) {
  e = pi;
};
ready
underscore.each
_.each(a, function(item) {
  item = pi;
});
ready
Better for loop
for (var i = a.length; i >= 0; i--) {
  e = pi;
}
ready
undrescore modified
_.each = _.forEach = function(obj, iterator, context) {
    if (obj == null) return;
    if (nativeForEach && obj.forEach === nativeForEach) {
      obj.forEach(iterator, context);
    } else if (obj.length === +obj.length) {
      for (var i = 0, l = obj.length; i < l; i++) {
        if (!!!context) {
          if (iterator(object[i], i) === breaker) return;
        } else {
          if (iterator.call(context, obj[i], i, obj) === breaker) return;
        }
      }
    } else {
      for (var key in obj) {
        if (_.has(obj, key)) {
          if (iterator.call(context, obj[key], key, obj) === breaker) return;
        }
      }
    }
  }

_.each(a, function(item) {
  item = pi;
});
ready

Revisions

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