copy array: concat vs simple loop and assignment

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    mixedarray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 'a', 'b', 'c',
    {
      foo: true
    }, [1, 2, 3], document, window]
};

Benchmark.prototype.teardown = function() {
    mixedarray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 'a', 'b', 'c',
    {
      foo: true
    }, [1, 2, 3], document, window]
};
</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
concat()
var copy = [].concat(mixedarray)
pending…
looping
var copy = []
for(var x=0, len = mixedarray.length;x < len;x++) {
     copy[x] = mixedarray[x];
}
pending…
slice
var copy = mixedarray.slice(0);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment