Traversing array vs arguments

JavaScript performance comparison

Test case created by Carlos Vázquez

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  traverseArgumentsDirectly = function() {
    for (var i = 0, le = arguments.length, x = 0; i < le; i++) {
      x += arguments[i];
    }
    return x;
  }
  
  traverseArgumentsAsArray = function() {
    for (var i = 0, array = Array.prototype.slice.call(arguments), le = array.length, x = 0; i < le; i++) {
      x += array[i];
    }
    return x;
  }
  
  var argumentArray = Array(1000);
  for(var i = 0; i < 1000; i++) {
    argumentArray[i] = 1;
  }
  
  traverseArrayAsArguments = function(arr) {
    return traverseArgumentsDirectly.apply(this, arr);
  }
  
  traverseArrayDirectly = function(arr) {
    for (var i = 0, le = arr.length, x = 0; i < le; i++) {
      x += arr[i];
    }
    return x;
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Traverse arguments directly
traverseArgumentsDirectly.apply(this, argumentArray)
pending…
Traverse arguments as an array
traverseArgumentsAsArray.apply(this, argumentArray)
pending…
Traverse array as arguments
traverseArrayAsArguments(argumentArray)
pending…
Traverse array directly
traverseArrayDirectly(argumentArray)
pending…

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

0 Comments