Array.prototype.slice vs Simple Loop

Benchmark created by gt on


Description

Just a quick test for Array.prototype.slice after learning it's very slow. Testing the idea put forth here: http://bonsaiden.github.com/JavaScript-Garden/

Preparation HTML

<script>
  var my_data=["one", 1, 3, "two", { foo : "bar" }], item=null;
  
  function check_slice( data ) {
     var args = Array.prototype.slice.call(arguments,0);
     if( args[4] && args[4].foo && args[4].foo == "bar") {
         item=args[4];
     }
  }
  
  function check_loop() {
      var a=arguments, args=[];
      for(var i=0; i<a.length; i++) {
          args.push( a[i] );
      }
     if( args[4] && args[4].foo && args[4].foo == "bar") {
         item=args[4];
     }
  }
  
  
  function check_array_instance() {
     var args = [].slice.call(arguments,0);
     if( args[4] && args[4].foo && args[4].foo == "bar") {
         item=args[4];
     }
  }
  
  
  function loop_decrement() {
      var _args=arguments, args=[], arg, i=_args.length-1;
      while( arg=_args[i] ) {
          args.shift( arg );
          i-=1;
      }
     if( args[4] && args[4].foo && args[4].foo == "bar") {
         item=args[4];
     }
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Array.prototype.slice Bake Off
check_slice( "one", 1, 3, "two", { foo : "bar" } );
ready
Simple Loop Bake Off
check_loop( "one", 1, 3, "two", { foo : "bar" } );
ready
Array Instance
check_array_instance( "one", 1, 3, "two", { foo : "bar" } );
ready
Loop Decrement
loop_decrement(  "one", 1, 3, "two", { foo : "bar" } );
ready

Revisions

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