Jquery each vs Underscore each vs For loops (v50)

Revision 50 of this benchmark created by Cindy on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js">
</script>

Setup

var arr = [];
    for (var i = 0; i < 10000; i++) {
      arr.push({
        Number: i
      });
    };
    var e = null;
    
    function Named(o, i) {
      e = o;
    };
    
    function ownEach(arr, fn) {
      for (var i = 0, len = arr.length; i <= len; i++) {
        fn(arr[i], i);
      };
    };
    
    function ownEachReversed(arr, fn) {
      for (var l = arr.length; l >= 0; l--) {
        fn(arr[l], i);
      };
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Jquery each
$.each(arr, function(i, o) {
  e = o;
});
ready
Underscore each
_.each(arr, function(o, i) {
  e = o;
});
ready
For normal
for (var i = 0, len = arr.length; i <= len; i++) {
  e = arr[i];
};
ready
For reversed
for (var l = arr.length; l >= 0; l--) {
  e = arr[l];
};
ready
Native array forEach
if (arr.forEach) {
  arr.forEach(function(o, i) {
    e = o;
  });
};
ready
Native array forEach with Named function
if (arr.forEach) {
  arr.forEach(Named);
};
ready
Underscore Named
_.each(arr, Named);
ready
ownEach
ownEach(arr, Named);
ready
ownEachReversed
ownEachReversed(arr, Named);
ready
while with pop
while (arr.length) {
 e = arr.pop();
}
ready
while with shift
while (arr.length) {
 e = arr.shift();
}
ready
while pop indexed
var l = arr.length,
    i = -1;
while (++i < l) {
 e = arr[i];
}
ready
while pop while creating copy
var newarr = [];

while (arr.length) {
  e = arr.pop();
  newarr.push(e);
}
ready
while pop with clone
var savedArr = _.clone(arr);

while (savedArr.length) {
  e = savedArr.pop();
}
ready
while pop with slice
var savedArr = arr.slice();

while (savedArr.length) {
  e = savedArr.pop();
}
ready

Revisions

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