Converting arguments to an array

JavaScript performance comparison

Revision 23 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
    var Array = window.Array;
    var push = Array.prototype.push;
    var slice = Array.prototype.slice;
    var unshift = Array.prototype.unshift;
  
    function arraySlice(array) {
      return slice.call(array);
    }
  
    function arrayConcat(array) {
      return [].concat(array);
    }
  
    function arrayPush(array) {
      var x = [];
      push.apply(x, array);
      return x;
    }
  
    function arrayUnshift(array) {
      var x = [];
      unshift.apply(x, array);
      return x;
    }
  
    function whileLoop(array) {
      var i = -1,
          l = array.length,
          x = Array(l);
   
      while (++i < l) {
        x[i] = array[i];
      }
      return x;
    }
  
    function byConstructor(array) {
      return Array.apply(null, array);
    }
  
    var largeArray = Array(1001).join("a").split("");
    var mediumArray = Array(101).join("a").split("");
    var smallArray = Array(6).join("a").split("");

};
</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
Array#slice
arraySlice(largeArray);
arraySlice(mediumArray);
arraySlice(smallArray);
pending…
Array#push
arrayPush(largeArray);
arrayPush(mediumArray);
arrayPush(smallArray);
pending…
Array#unshift
arrayUnshift(largeArray);
arrayUnshift(mediumArray);
arrayUnshift(smallArray);
pending…
Array
byConstructor(largeArray);
byConstructor(mediumArray);
byConstructor(smallArray);
pending…
while loop
whileLoop(largeArray);
whileLoop(mediumArray);
whileLoop(smallArray);
pending…
concat
arrayConcat(largeArray);
arrayConcat(mediumArray);
arrayConcat(smallArray);
pending…

Compare results of other browsers

Revisions

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

0 Comments