Underscore.each vs jQuery.each vs. for loop (v233)

Revision 233 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>
  var pi = Math.PI
  var a = new Array(100000),
      e;

  var ArrayIterator = function(array) {
      this.array = array;
      this.index = -1;
      };

  ArrayIterator.prototype.next = function() {
    return this.index < (this.array.length - 1);
  };

  ArrayIterator.prototype.current = function() {
    this.index += 1;
    return this.array[this.index];
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery.each
$.each(a, function() {
  e = 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) {
  e = pi;
});
ready
good old for loop (decrementing)
var i = a.length;
for (; i > 0; i--) {
  e = pi;
};
ready
another loop
var len = a.length,
    i = 0;
for (; i < len; i += 1) {
  e = pi
};
ready
Iterator pattern
var it = new ArrayIterator(a);
while (it.next()) {
  e = it.current();
}
ready
call in loop
var func = function() {
    e = pi;
};
for (var i = 0, len = a.length; i < len; ++i) {
  func();
}
ready
indirect call in loop
var func = function() {
    e = pi;
};
for (var i = 0, len = a.length; i < len; ++i) {
  func.call();
}
ready
apply in loop
var func = function() {
    e = pi;
};
var args = [];
for (var i = 0, len = a.length; i < len; ++i) {
  func.apply(this, args);
}
ready

Revisions

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