copy-loop vs. Array.slice (v19)

Revision 19 of this benchmark created 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 = new Array(1024);
    for (var i = 0; i < 1024; i++){
    
    list[i] = Math.random();
    
    }
    var copy = [];
    
    var len = list.length;

Test runner

Ready to run.

Testing in
TestOps/sec
Copy-Loop
for ( var i=0;i<len; i++ ) copy.push(list[i]);
ready
Array-Slice
var 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();
for ( var i=0;i<list.length; i++ ) copy[i] = list[i];
ready
map
var copy = list.map(function(item){return item;});
ready

Revisions

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