Compare for loop vs while loop

JavaScript performance comparison

Revision 25 of this test case created by Stefan Feldbinder

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const a = [];
  for(let 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
while reverse
const length = a.length;
while(--length){}
pending…
for cached 1
function sum_o1(a,b,c,d){
var ab = a + b;
var bc = b + c;
var cd = c + d;
   return ab+bc+cd;
}
pending…
while basic
let i = 0;
while(i < a.length){ ++i; }
pending…
for basic reverse
for(let i = a.length; --i;){}
pending…
for basic
for(let i = 0; i < a.length; ++i){}
pending…
for cached 2
function sum_o1(a,b,c,d, ab, bc, cd){
ab = a + b;
bc = b + c;
cd = c + d;
   return ab+bc+cd;
}
pending…
for cached 1 reverse
const length = a.length;
for(let i = length; --i;){}
pending…
for cached 2 reverse
const length = a.length;
for(; --length;){}
pending…
while chached
const length = a.length,
let i = 0;
while(i < length){ ++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