Underscore.each vs jQuery.each vs. for loop vs. forEach (v103)

Revision 103 of this benchmark created on


Description

All test assume you want to do something with the respective item in the array.

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 a = new Array(1000), o;

var f_each = function(a, fn) {
    for (var i = 0, l = a.length; i < l; i++) {
        fn(a[i], i);
    }
}


</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery.each
$.each(a, function(i, t) {
  o = t;
});
ready
good old for loop
for (var i = 0, l = a.length; i < l; i++) {
  o = a[i];
};
ready
underscore.each
_.each(a, function(t, i) {
  o = t;
});
ready
good old for loop (decrementing)
for (var i = a.length; i > 0; i--) {
  o = a[i];
};
ready
forEach
a.forEach(function(t, i) {
  o = t;
});
ready
functional
f_each(a, function(t, i) {
    o = t;
});
ready

Revisions

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