copy-loop vs. Array.slice (v17)

Revision 17 of this benchmark created by hh 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(list.length);
for (var i = 0; i < list.length; i++) copy.push(list[i]);
ready
Array-Slice
var slice = Array.prototype.slice;
copy = slice.call(list);
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(list.length);
for (var i = 0; i < list.length; 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.