Converting arguments to an array (v32)

Revision 32 of this benchmark created by Niklas Lindgren 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 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);
    }
  
    function spreads(array) {
      return [...array];
    }
  
    function from(array) {
      return Array.from(array);
    }
  
    function arrayOf(array) {
      return Array.of.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#unshift
arrayUnshift(largeArray);
arrayUnshift(mediumArray);
arrayUnshift(smallArray);
ready
while loop
whileLoop(largeArray);
whileLoop(mediumArray);
whileLoop(smallArray);
ready
Array#slice
arraySlice(largeArray);
arraySlice(mediumArray);
arraySlice(smallArray);
ready
Array#push
arrayPush(largeArray);
arrayPush(mediumArray);
arrayPush(smallArray);
ready
Array
byConstructor(largeArray);
byConstructor(mediumArray);
byConstructor(smallArray);
ready
array spreads
spreads(largeArray);
spreads(mediumArray);
spreads(smallArray);
ready
Array.from
from(largeArray);
from(mediumArray);
from(smallArray);
ready
Array#concat
arrayConcat(largeArray);
arrayConcat(mediumArray);
arrayConcat(smallArray);
ready
Array#of
arrayOf(largeArray);
arrayOf(mediumArray);
arrayOf(smallArray);
ready

Revisions

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