Loops (v34)

Revision 34 of this benchmark created by FrancescoMM on


Description

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 HTML

<script>
  var arr = new Array(800);
  for (var i = 0; i < arr.length; ++i) arr[i] = (Math.random() * 10001) | 0;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
  arr[i] += 0;
  i++;
};
ready
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
  arr[i] += 0;
  i++;
};
ready
Reverse while loop
var i = arr.length;
while (i) {
  arr[--i] += 0;
}
ready
Reverse while loop without implicit ToBoolean
var i = arr.length;
while (i > 0) {
  arr[--i] += 0;
}
ready
Reverse do … while loop
var i = arr.length - 1;
if (i >= 0) do {
  arr[i] += 0;
} while (i--);
ready
Reverse for loop
for (var i = arr.length; i--;) {
  arr[i] += 0;
};
ready
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
  arr[i] += 0;
};
ready
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i] += 0;
};
ready
Cool guy loop
for (var i = -1; ++i < arr.length;) {
  arr[i] += 0;
};
ready
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
  arr[i] += 0;
};
ready
Native Array#forEach implementation
arr.forEach(function(x) {
  x += 0;
});
ready
Native Array#forEach implementation with named function
function foo(x) {
  x += 0;
};
arr.forEach(foo);
ready
For in
for (var it in arr) {
  if (arr.hasOwnProperty(it) && isFinite(it)) {
    arr[it] += 0;
  }
};
ready
Increment in sentinel
var i = 0;
for (; arr[i++];) {
  arr[i] += 0;
}
ready
Decrement
var i = arr.length,
    item;

for (; i--; item = arr[i]) {
  arr[i] += 0;
}
ready
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;
}
ready
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;
}
ready
Reverse do … while loop without implicit ToBoolean
var i = arr.length - 1;
do {
  arr[i] += 0;
} while (i-- > 0);
ready

Revisions

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