copy-loop vs. Array.slice (v5)

Revision 5 of this benchmark created by 1Pupik1989 on


Description

The performance difference between copying an array by hand and copying it by slicing it (completely). This is for short arrays.

Setup

var list = ['a','b','c'];

Test runner

Ready to run.

Testing in
TestOps/sec
Copy-Loop
var copy = new Array();
for ( var i=0;i<list.length; i++ ) copy.push(list[i]);
ready
Array-Slice
var copy = list.slice();
ready
while-copy-loop
var i = list.length;
var copy = new Array(i);
while (i--) copy.push(list[i]);
ready
While-Paul
var i = list.length;
var copy = Array(i);
while (i--) {
  copy[i] = list[i];
}
ready
for loop no method call
var copy = new Array();
for ( var i=0;i<list.length; i++ ) copy[i] = list[i];
ready
for loop cached
var copy = [];
for ( var i=0,length=list.length;i<length; i++ ){
  copy[i] = list[i];
}
ready
for loop cached, push
var copy = [];
for ( var i=0,length=list.length;i<length; i++ ){
  copy.push(list[i]);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.