Loops

JavaScript performance comparison

Revision 34 of this test case created by FrancescoMM

Info

Added for( .. in ..), set array to random integer values, and set basic operation to adding zero, added some corrections from revison 23, added some tests from other revisions, cleaned up a few things

Preparation code

<script>
  var arr = new Array(800);
  for (var i = 0; i < arr.length; ++i) arr[i] = (Math.random() * 10001) | 0;
</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
while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
  arr[i] += 0;
  i++;
};
pending…
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
  arr[i] += 0;
  i++;
};
pending…
Reverse while loop
var i = arr.length;
while (i) {
  arr[--i] += 0;
}
pending…
Reverse while loop without implicit ToBoolean
var i = arr.length;
while (i > 0) {
  arr[--i] += 0;
}
pending…
Reverse do … while loop
var i = arr.length - 1;
if (i >= 0) do {
  arr[i] += 0;
} while (i--);
pending…
Reverse for loop
for (var i = arr.length; i--;) {
  arr[i] += 0;
};
pending…
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
  arr[i] += 0;
};
pending…
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i] += 0;
};
pending…
Cool guy loop
for (var i = -1; ++i < arr.length;) {
  arr[i] += 0;
};
pending…
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
  arr[i] += 0;
};
pending…
Native Array#forEach implementation
arr.forEach(function(x) {
  x += 0;
});
pending…
Native Array#forEach implementation with named function
function foo(x) {
  x += 0;
};
arr.forEach(foo);
pending…
For in
for (var it in arr) {
  if (arr.hasOwnProperty(it) && isFinite(it)) {
    arr[it] += 0;
  }
};
pending…
Increment in sentinel
var i = 0;
for (; arr[i++];) {
  arr[i] += 0;
}
pending…
Decrement
var i = arr.length,
    item;

for (; i--; item = arr[i]) {
  arr[i] += 0;
}
pending…
Old ’n’ busted for loop, caching the length, post increment (i++))
var l = arr.length
for (var i = 0, len = l; i < len; i++) {
  arr[i] += 0;
}
pending…
Duff would be proud
for (var i = arr.length; (i -= 8)>0;) {
  arr[i] += 0;
  arr[i - 1] += 0;
  arr[i - 2] += 0;
  arr[i - 3] += 0;
  arr[i - 4] += 0;
  arr[i - 5] += 0;
  arr[i - 6] += 0;
  arr[i - 7] += 0;
}
pending…
Reverse do … while loop without implicit ToBoolean
var i = arr.length - 1;
do {
  arr[i] += 0;
} while (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. Here’s a list of current revisions for this page:

2 comments

FrancescoMM (revision owner) commented :

Added for( in ) loop wich looks faster

FrancescoMM (revision owner) commented :

Setting all values to floating point numbers instead of nulls changed it all

Add a comment