Array copy:slice vs concat

JavaScript performance comparison

Test case created by Keilin Olsen

Info

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

Preparation code

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

Benchmark.prototype.teardown = function() {
    source = [];
    dest = [];
};
</script>

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();
pending…
Copy with concat
dest = [].concat(source);
pending…

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

Compare results of other browsers

0 comments

Add a comment