Compare for loop vs while loop

JavaScript performance comparison

Revision 16 of this test case created by

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 test inside
for (var i = 0; i < a.length; i++) {
  if (a[i] === 9999) break;
}
pending…
for cached 1 test inside
var length = a.length;
for (var i = 0; i < length; i++) {
  if (a[i] === 9999) break;
}
pending…
for basic test header
for (var i = 0; i < a.length && a[i] !== 9999; i++) {}
pending…
for cached 1 test header
var length = a.length;
for (var i = 0; i < length && a[i] !== 9999; i++) {}
pending…
for cached 1 reverse test inside
var length = a.length;
for (var i = length; i--;) {
  if (a[i] === 0) break;
}
pending…
for cached 2 reverse
var length = a.length;
for (; length-- && a[i] !== 0;) {}
pending…
while basic
var i = 0;
while (i < a.length && a[i] !== 9999) {
  i++;
}
pending…
while chached
var length = a.length,
  i = 0;
while (i < length && a[i] !== 9999) {
  i++;
}
pending…
while reverse
var length = a.length;
while (length-- && a[i] !== 0) {}
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