Slow loop (v16)

Revision 16 of this benchmark created by Alan on


Description

Comparing the "slow loop" argument from http://www.sitepoint.com/google-closure-how-not-to-write-javascript/

Preparation HTML

<script>
  var arr = [];
  for (var i = 0; i < 10000; i++) {
    arr.push(i);
  }
  
  var highest = -1;
  var lowest = -1;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
"Slow" loop
for (var i = 0; i < arr.length; ++i) {
  highest = i;
}
ready
"Fast" loop
for (var i = 0, len = arr.length; i < len; ++i) {
  highest = i;
}
ready
"Fastest" loop
var i = arr.length;
while (i--) {
  lowest = i;
}
ready
Reverse "Fastest" loop
var i = -arr.length;
while (i++) {
  lowest = i;
}
ready
Reverse "Fast" loop
for (var i = arr.length - 1; i >= 0; --i) {
  lowest = i;
}
ready
Faster than "Fast" loop?
var m = arr.length-1
for (var i=m; i >= 0; --i) {
  lowest = i;
}
ready
Another potential faster one?
for (var i = arr.length - 1; i--;) {
  lowest = i;
}
ready

Revisions

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