Fastest Loop

JavaScript performance comparison

Revision 28 of this test case created

Info

do not use .pop() on the same array over and over, because it is already empty....

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = new Array();
    for(var i=0;i<=1000000;i++) a.push(i);
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
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 = Object.create(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 b = Object.create(a);
var l = b.length - 1;
while(l >= 0)
{
b.splice(l--, 1)[0];
}
pending…
While (splice)
var b = Object.create(a);
var l = b.length;
while(l)
{
b.splice(--l, 1)[0];
}
pending…
If Do While (splice)
var b = Object.create(a);
var l = b.length;
if (l) {
  do {
    b.splice(--l, 1)[0];
  } while (l);
}
pending…
If Do While (pop)
var b = Object.create(a);
var l = b.length;
if (l) {
  do {
    b.pop();
    --l;
  } while (l);
}
pending…
If Do While (length)
var b = Object.create(a);
var l = b.length;
if (l) {
  do {
    b.length -= 1;
    --l;
  } while (l);
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment