Duplicate array (slice vs concat) (v10)

Revision 10 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 = [],
      res = [];
    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
for
for (var i = 0, len = arr.length; i < len; res[i] = arr[i++]);
ready

Revisions

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