Loop Length and Var init

JavaScript performance comparison

Test case created by gt

Info

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 code

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Local Variable Store
cached_loop( long_arr );
pending…
Check length Each Time
uncached_loop( long_arr );
pending…
Shorthand Loop Syntax
shorthand_syntax_loop( long_arr );
pending…

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

Compare results of other browsers

0 comments

Add a comment