Convert arguments into an Array

JavaScript performance comparison

Test case created by Baptiste Fontaine

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var convert = function(obj) {
             if (!(obj.length >= 0)) { // undefined or < 0
                     return null;
             }
             var i,a = [], len = obj.length;
             for (i=0; i<len; i++) {
                     a.push(obj[i]);
             }
             return a;
    },
   
    f1 = function() {return convert(arguments);},
    f2 = function() {return [].slice.apply(arguments);},
    f3 = function() {return Array.prototype.slice.apply(arguments);};
};
</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
Laborious way
f1();
f1(1,2,3);
f1(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,7,2);
pending…
Slice 1
f2();
f2(1,2,3);
f2(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,7,2);
pending…
Slice 2
f3();
f3(1,2,3);
f3(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,7,2);
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