For Loop Comparisons

JavaScript performance comparison

Revision 4 of this test case created by Ates Goral

Preparation code

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

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

Compare results of other browsers


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.

Comment form temporarily disabled.

Add a comment