Array clone slice vs for loop

JavaScript performance comparison

Revision 6 of this test case created by Mikhail Shestakov

Info

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

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
a=['a','b','c','d','e','f'];
b= a.slice();
for(var i=0,len=b.length;i<len;i++){
   if(typeof b[i] == 'string') "sdf";
}
pending…
for
a=['a','b','c','d','e','f'];
for(var i=0,b=[],len=a.length;i<len;i++){
   if(typeof a[i] == 'string')b[i]=a[i];
}
pending…
slice apply
a=['a','b','c','d','e','f']
b= [].slice.apply( a )
pending…
slice call
a=['a','b','c','d','e','f']
b= [].slice.call( a )
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