Fastest Loop

JavaScript performance comparison

Revision 24 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

};
</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
for (var i = 0; i < a.length; i++) {}
pending…
For (cache length)
for (var i = 0, l = a.length; i < l; i++) {}
pending…
For (reverse)
for (var i = a.length; i--;) {}
pending…
For In
for (var i in a) {}
pending…
For (exists)
for (var i = 1; a[i]; i++) {}
pending…
While (imitate loop)
var i = 0; while (i < a.length) { i++; }
pending…
While (imitate loop) (cache length)
var i = 0, l = a.length; while (i < l) { i++; }
pending…
While (reverse)
var i = a.length; while (i--) {}
pending…
Do While (reverse)
var i = a.length - 1; do {} while (i--);
pending…
While (pop)
var i = a, l = i.length; while (l--) { i.pop() }
pending…
.forEach()
a.forEach(function() {});
pending…
For (reverse - optimized?)
for (var i = a.length; i > 0; i -= 1) { }
pending…
For with splice
var l = a.length - 1; 
while(l >= 0) 
{
a.splice(l--, 1)[0];
}
pending…
While (splice)
var l = a.length; 
while(l) 
{
a.splice(--l, 1)[0];
}
pending…
If Do While (splice)
var l = a.length; 
if (l) { 
  do {
    a.splice(--l, 1)[0];
  } while (l);
}
pending…
If Do While (pop)
var l = a.length; 
if (l) { 
  do {
    a.pop();
    --l;
  } while (l);
}
pending…
If Do While (length)
var l = a.length; 
if (l) { 
  do {
    a.length -= 1;
    --l;
  } while (l);
}
pending…
If Do While (pop)
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