Loops (v35)

Revision 35 of this benchmark created by Elijah Manor on


Description

Added for( .. in ..), set array to random floating values, added some corrections from revison 23, added some tests from other revisions

Preparation HTML

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

for (; i--; item = arr[i]) {
  arr[i] += 0.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.0;
}
ready
Duff would be proud
for (var i = arr.length; i -= 8;) {
  arr[i] += 0.0;
  arr[i - 1] += 0.0;
  arr[i - 2] += 0.0;
  arr[i - 3] += 0.0;
  arr[i - 4] += 0.0;
  arr[i - 5] += 0.0;
  arr[i - 6] += 0.0;
  arr[i - 7] += 0.0;
}
ready
Reverse Array 1st
arr.reverse();
var i = arr.length;
while ( i > 0 ){
 arr[ i ] += 0.0;
 i--;
}
ready
While backwards
var i = arr.length;
while ( i > 0 ) {
 arr[ i ] += 0.0;
 i--;
}
ready

Revisions

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