Loop Length and Var init

Benchmark created by gt on


Description

This is a simple bake off for checking loop speed with pre-initalized and cached vars vs more 'free form, on the fly loops'

Preparation HTML

<script>
  var long_arr=new Array(100), item;
  
  function cached_loop(arr) {
      var lst=arr, l=lst.length, i=0;
      for( ; i<l; i++) {
         item=lst[i]
      }
  }
  
  
  function uncached_loop(arr) {
     for(var i=0; i<arr.length; i++) {
         item=arr[i];
     }
  }
  
  function shorthand_syntax_loop(arr) {
      for(var i=0, l=arr.length; i<l; i++) {
          item=arr[i]
      }
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Local Variable Store
cached_loop( long_arr );
ready
Check length Each Time
uncached_loop( long_arr );
ready
Shorthand Loop Syntax
shorthand_syntax_loop( long_arr );
ready

Revisions

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