Array clone slice vs for loop

JavaScript performance comparison

Revision 5 of this test case created by derry

Info

It seems that the fastest way to clone an array is array.concat()

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    Array.prototype.Clone = function() {
    var a = this;
    var b = [];
    for (var i = 0, n = a.length; i < n; ++i)
    {
        b[i] = a[i];
    }
    return b;
    };
   
    var a=['a','b','c','d','e','f']
   
};
</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
slice
b= a.slice()
pending…
for
for(var i=0,b=[],len=a.length;i<len;i++){ b[i]=a[i] }
pending…
slice apply
b= [].slice.apply( a )
pending…
slice call
b= [].slice.call( a )
pending…
concat
b= a.concat()
pending…
concat 2
b= a.concat([])
pending…
concat 3
b= [].concat(a)
pending…
prototype for
b = a.Clone();
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