for(;;;) vs forEach vs $.each

JavaScript performance comparison

Revision 24 of this test case created by Todd

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var i,
        cachedFunc = function(i){ i / 2; };
   
    window.arr = [];
   
    for (i = 0; i < 10000; i += 1) {
      arr.push(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
for(;;;)
var ii = 0, len = arr.length;

for (ii = 0; ii < len; ii += 1) {
  ii / 2;
}
pending…
native forEach
arr.forEach(function (i) {
  i / 2;
});
pending…
$.each
$.each(arr, function (i) {
  i / 2;
});
pending…
native forEach (cached func)
arr.forEach(cachedFunc);
pending…
$.each (cached func)
$.each(arr, cachedFunc);
pending…
for(;;;) (self func)
var ii = 0, len = arr.length;

for (ii = 0; ii < len; ii += 1) {
  (function(i){ i / 2; }(ii));
}
pending…
reverse for(;;;)
var ii, len = arr.length;

for (ii = len - 1; ii >= 0; ii--) {
  ii / 2;
}
pending…
for(;;;) (no var caching)
for (var ii = 0; ii < arr.length; ii += 1) {
  ii / 2;
}
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