Array.prototype.push.apply vs concat (v52)

Revision 52 of this benchmark created by test on


Description

This page tests speed of two different methods for array concatenation. Array method concat() has no side effects (it creates a new array to store its result in), while Array.prototype.push.apply(A, [1, 2, ...]) extends the array A.

Preparation HTML

<script>
  var x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  var arrPush = Array.prototype.push;
var push_array = function(x,b){
var a = x.slice(0),
    bl = b.length,
    al = a.length,
    i = 0;
  
while (i < bl) {
  a[al++] = b[i++];
}
return a;
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
concat
var a = x.slice(0),
    b = y.slice(0);

a = a.concat(b);
ready
Array#push.apply
var a = x.slice(0),
    b = y.slice(0);

Array.prototype.push.apply(a, b);
ready
Array#push with loop
var a = x.slice(0),
    b = y.slice(0),
    i = 0,
    c = b.length;

for (; i < c; ++i) {
  a.push(b[i]);
}
ready
Cached Array#push
var a = x.slice(0),
    b = y.slice(0);

arrPush.apply(a, b);
ready
Splice
var a = x.slice(0),
    b = y.slice(0);
a.splice.apply(b, [b.length, 9e9].concat(b))
ready
Array#push with loop while
var a = x.slice(0),
    b = y.slice(0),
    c = b.length;

while (c--) {
  a.push(b[c]);
}
ready
var a = x.slice(0),
    b = y.slice(0),
    bl = b.length,
    al = a.length,
    i = 0;
  
while (i < bl) {
  a[al++] = b[i++];
}
ready
last test
push_array(x,y);
ready
apply
a = x.slice(0);
arrPush.apply(a, y);
ready
just slice
a = x.slice(0);
ready

Revisions

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