Compare for loop vs while loop

JavaScript performance comparison

Revision 5 of this test case created by Mark

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
for (var i = 0; i < a.length; i++) {}
pending…
for cached 1
var length = a.length;
for (var i = 0; i < length; i++) {}
pending…
for cached 2
var length = a.length,
  i = 0;
for (; i < length; i++) {}
pending…
for basic reverse
for (var i = a.length; i--;) {}
pending…
for cached 1 reverse
var length = a.length;
for (var i = length; i--;) {}
pending…
for cached 2 reverse
var length = a.length;
for (; length--;) {}
pending…
while basic
var i = 0;
while (i < a.length) {
  i++;
}
pending…
while chached
var length = a.length,
  i = 0;
while (i < length) {
  i++;
}
pending…
while reverse
var length = a.length;
while (length--) {}
pending…
while cached 2
var length = a.length,
  i = -1;
while (++i < length) {}
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