Slow loop (v31)

Revision 31 of this benchmark created on


Description

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

Preparation HTML

<script>

</script>

Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
"Slow" loop
for (var i = 0; i < arr.length; i++) {
  highest = i;
}
console.log("Test 1: " + highest);
ready
"Fast" loop
for (var i = 0, len = arr.length; i < len; i++) {
  highest = i;
}
console.log("Test 2: " + highest);
ready
"Fastest" loop
var len = arr.length,
  i;
for (i = -1; ++i < len;) {
  highest = i;
}
console.log("Test 3: " + highest);
ready
"Fastestest loop"
for (var i = arr.length; i--;) {
  highest = i;
}
console.log("Test 3: " + highest);
ready

Revisions

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