Converting arguments to an array

JavaScript performance comparison

Revision 7 of this test case created by damien maillard and last updated

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 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 unknown unknown
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…

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. Here’s a list of current revisions for this page:

0 comments

Add a comment