Compare for loop vs while loop

JavaScript performance comparison

Revision 28 of this test case created by Luca Gesmundo

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = [];
  for(var i = 1; i <= 10000; i++) a.push(i);

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for basic reverse
for(var i = a.length; i--;){}
pending…
for cached 2 reverse
var length = a.length;
for(; length--;){}
pending…
while chached
var length = a.length,
i = 0;
while(i < length){ i++; }
pending…
for cached 2
var length = a.length,
i = 0;
for(; i < length; i++){}
pending…
for cached 1 reverse
var length = a.length;
for(var i = length; i--;){}
pending…
for basic
for(var i = 0; i < a.length; i++){}
pending…
for cached 1
var length = a.length;
for(var i = 0; i < length; i++){}
pending…
while reverse
var length = a.length;
while(length--){}
pending…
empty empty
for (;;) {}
pending…
while basic
var i = 0;
while(i < a.length){ i++; }
pending…
while true
const length = a.length;
let i = 0;
while(true) {
  if (i === length) break;
  i++;
}
pending…
for empty
const length = a.length;
let i = 0;
for(;;) {
  if (i === length) break;
  i++;
}
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.

0 Comments