copy-loop vs. Array.slice (v25)

Revision 25 of this benchmark created by Tri 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','d','e','f','g','h','i','j'];

Test runner

Ready to run.

Testing in
TestOps/sec
Copy-Loop
var L=list.length,copy = new Array(L);
for ( var i=0;i<L; i++ ) copy.push(list[i]);
ready
Array-Slice
var copy = list.slice();
ready
while
var i = list.length,copy = new Array(i);
while (i--) {
  copy[i] = list[i];
}
ready
Loop2
var L=list.length,copy = new Array(L);
for ( var i=0;i<L; i++ ) copy[i] = list[i];
ready

Revisions

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