Duplicate array (slice vs concat) (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script>
function arrayCopy( arr ) {
var ret = new Array(arr.length);
for( var i = 0, len = arr.length; i < len; ++i ) {
    ret[i] = arr[i];
}
return ret;
}
</script>

Setup

var arr = [];
    for (var i=0; i<12500; i++) {
        arr.push(1, 2, 3, 4);
    }
    // 12500x8 = 1 000 000 elements

Test runner

Ready to run.

Testing in
TestOps/sec
Concat
var res = arr.concat();
ready
Slice
var res = arr.slice()
ready
while [ ]
var len = arr.length, i=-1, res = [];
while (++i < len) {
    res[i] = arr[i];
}
ready
while push
var i = arr.length+1, res = [];
while (--i) {
    res.push(arr[i]);
}
ready
arrayCopy
var res = arrayCopy(arr);
ready

Revisions

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