Array copy:slice vs concat

JavaScript performance comparison

Test case created by Keilin Olsen


Testing whether a piecewise copy with slice() is faster than empty array with concat()

Preparation code

Benchmark.prototype.setup = function() {
    var source = [];
    var dest = [];
    for (var i=0;i<40000;i++) { source.push(i); }

Benchmark.prototype.teardown = function() {
    source = [];
    dest = [];

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Copy with slice
dest = source.slice();
Copy with concat
dest = [].concat(source);

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

Compare results of other browsers


Comment form temporarily disabled.

Add a comment