Duplicate array (slice vs concat) (v8)

Revision 8 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<500; i++) {
        arr.push(1, 2, 3, 4);
    }

Teardown


    var res = [];
  

Test runner

Ready to run.

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

Revisions

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