Converting arguments to an array (v7)

Revision 7 of this benchmark created by damien maillard on


Setup

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 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("");

Test runner

Ready to run.

Testing in
TestOps/sec
Array#slice
arraySlice(largeArray);
arraySlice(mediumArray);
arraySlice(smallArray);
ready
Array#push
arrayPush(largeArray);
arrayPush(mediumArray);
arrayPush(smallArray);
ready
Array#unshift
arrayUnshift(largeArray);
arrayUnshift(mediumArray);
arrayUnshift(smallArray);
ready
Array
byConstructor(largeArray);
byConstructor(mediumArray);
byConstructor(smallArray);
ready
while loop
whileLoop(largeArray);
whileLoop(mediumArray);
whileLoop(smallArray);
ready

Revisions

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