copy simple array (v6)

Revision 6 of this benchmark created by dmi3y on


Description

Quickest way to get a byval copy of an array of simple values - will NOT copy immutable objects in arrays byval e.g. nested arrays / objects etc get copied BYREF.

Setup

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9],
        b = null;

Test runner

Ready to run.

Testing in
TestOps/sec
concat
b = a.concat();
ready
slice w/index
b = a.slice(0);
ready
slice w/o index
b = a.slice();
ready
[]
b = [].concat(a);
ready
new Array
b = new Array(a.join());
ready
loop
var i = a.length,
    b = [];
for (;(i);) {
  --i;
  b.push(a[i]);
}
ready
JSON parser (object's cloning too)
b = JSON.parse(JSON.stringify(a));
ready
JSON parser with toSource
b = JSON.parse(a.toSource());
ready

Revisions

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