For Loop Comparisons

JavaScript performance comparison

Revision 4 of this test case created by Ates Goral

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [];
    for (var i = 0; i < 2000; i++) {
      arr[i] = i * 2;
    }
   
};
</script>

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 In
for( var y in arr ) {
  var m = arr[y];
}
pending…
for i = 0;
for( var y = 0; y < arr.length; y++){
 var m = arr[y];
}
pending…
i = 0 cached
for( var y = 0, k = arr.length; y < k; y++){
 var m = arr[y]
}
pending…
forEach
arr.forEach(function (v) {
 var m = v;
});
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:

1 comment

Ates Goral (revision owner) commented :

And BTW, the "for in" test isn't even valid. You should not iterate over items of an Array with "for in", which will also go through the Array object's non-index properties.

Add a comment