Underscore.each vs jQuery.each vs. for loop vs. forEach

JavaScript performance comparison

Revision 103 of this test case created

Info

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

Preparation code

<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>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

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

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment