Converting arguments to an array

JavaScript performance comparison

Revision 21 of this test case created by Calvin

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var resultLarge, resultSmall, resultMedium;  
  
  
    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#push
resultLarge = [];
Array.prototype.push.apply(resultLarge,largeArray);
resultMedium = []
Array.prototype.push.apply(resultMedium,mediumArray);
resultSmall = []
Array.prototype.push.apply(resultSmall,smallArray);
pending…
Array#unshift
resultLarge = [];
Array.prototype.unshift.apply(resultLarge,largeArray);
resultMedium = []
Array.prototype.unshift.apply(resultMedium,mediumArray);
resultSmall = []
Array.prototype.unshift.apply(resultSmall,smallArray);
pending…
Array
resultLarge = Array.call(null, largeArray);
resultMedium = Array.call(null, mediumArray);
resultSmall = Array.call(null, smallArray);
pending…
while loop
var i, len;

i = -1;
len = largeArray.length;
resultLarge = new Array(len);
while (++i < len) {
   resultLarge[i] = largeArray[i];
}

i = -1;
len = mediumArray.length;
resultMedium = new Array(len);
while (++i < len) {
   resultMedium[i] = mediumArray[i];
}

i = -1;
len = smallArray.length;
resultSmall = new Array(len);
while (++i < len) {
   resultSmall[i] = smallArray[i];
}
pending…
Array#concat
resultLarge = largeArray.concat();
resultMedium = mediumArray.concat();
resultSmall = smallArray.concat();
if (resultLarge === largeArray) {
    throw new Error('supposed to be different');
}
pending…
Array#slice
resultLarge = largeArray.slice();
resultMedium = mediumArray.slice();
resultSmall = smallArray.slice();
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