Compare for loop vs while loop

JavaScript performance comparison

Revision 15 of this test case created by Oleg Buylov

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