Loops (v110)

Revision 110 of this benchmark created on


Preparation HTML

<script>
var arr = new Array(100);
for (var j = 0, n = arr.length; j < n; j++) {
  arr[j] = Math.random();
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
while loop that imitates a for loop
var z = 0;
var i = 0;
while (i < arr.length) {
  z += 1;
  i++;
};
ready
while loop that imitates a for loop, caching the length
var z = 0;
var i = 0,
  len = arr.length;
while (i < len) {
  z += 1;
  i++;
};
ready
Reverse while loop
var z = 0;
var i = arr.length;
while (i--) {
  z += 1;
};
ready
Reverse while loop without implicit ToBoolean
var z = 0;
var i = arr.length;
while (i-- > 0) {
  z += 1;
};
ready
Reverse do … while loop
var z = 0;
var i = arr.length;
do {
  z += 1;
} while (i--);
ready
Reverse for loop
var z = 0;
for (var i = arr.length; i--;) {
  z += 1;
};
ready
Old ’n’ busted for loop
var z = 0;
for (var i = 0; i < arr.length; ++i) {
  z += 1;
};
ready
Old ’n’ busted for loop, caching the length
var z = 0;
for (var i = 0, len = arr.length; i < len; ++i) {
  z += 1;
};
ready
Cool guy loop
var z = 0;
for (var i = -1; ++i < arr.length;) {
  z += 1;
};
ready
Cool guy loop, caching the length
var z = 0;
for (var i = -1, len = arr.length; ++i < len;) {
  z += 1;
};
ready
Native Array#forEach implementation
var z = 0;
arr.forEach(function(x) {
  z += x;
});
ready
Native Array#forEach implementation with named function
var z = 0;

function foo(x) {
  z += x;
};
arr.forEach(foo);
ready
Reverse while loop without implicit ToBoolean (2)
/* Joerg_Schaper@gmx.de 2013/06/16 */
var z = 0;
var i = arr.length;
while (i > 0) {
  z += 1;
  --i;
};
ready
Reverse while loop without implicit ToBoolean (3)
/* Joerg_Schaper@gmx.de 2013/06/16 */
var z = 0;
var i = arr.length;
while (i > 0) {
  z += 1;
  i--;
};
ready
var z = 0;
var i = arr.length;
while (--i > -1) {
  z += 1;
};
ready
For .. in
var z = 0;
for (var i in arr) {
  z += 1;
};
ready
For loop with variables initialized first
var z = 0,
  i = -1,
  len = arr.length;
for (; ++i < len;) {
  z += 1;
};
ready
var z = 0;
for (var i = 0; i < arr.length; i++) {
  z += 1;
}
ready
var i = -1,
  l = arr.length,
  z = 0;
while (++i < l) {
  z += 1;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.