Different kinds of loop (v6)

Revision 6 of this benchmark created on


Description

In this revision I've updated the tests to call a function rather than count++; which is then not used.

I did this as in theory it's possible that some browsers will be able to see this variable is unused and optimise out that code so it's never run.

A real world app is unlikely to contain unused code, so this could skew the tests. - Jamie Mason / @GotNoSugarBaby

Preparation HTML

<script>
  var arr = [], obj = {};
  
  (function ()
  {
      for( var i = 0; i < 1000; i++ ) {
          arr[i] = 'value' + i;
          obj[i] = 'value' + i;
      }    
  }());
  
  function someFn (ix)
  {
      return ix * 5 + 1 / 3 * 8;
  }
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
var i in arr
for( var i in arr ) {
    someFn(i);
}
ready
for i < arr.length (without caching)
for( var i = 0; i < arr.length; i++ ) {
    someFn(i);
}
ready
for i < arr.length (with caching)
for( var i = 0, max = arr.length; i < max; i++ ) {
    someFn(i);
}
ready

Revisions

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