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

Revision 2 of this benchmark created 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 len = 20150;
  var x = new Array(len),
      y = new Array(150),
      z = new Array(len*2);
  for (var i = 0; i < len; i++) {
    x[i] = i;
  }
  
  for (var i = 0; i < 2*len; i++) {
    z[i] = i;
  }
  
  for (var i = 0; i < 150; i++) {
    x[i] = i;
  }
</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.prototype.push.apply
var a = x.slice(0),
    b = y.slice(0);

Array.prototype.push.apply(a, b);
ready
concat 2
var a = z.slice(0),
    b = y.slice(0);

a = a.concat(b);
ready
Array.prototype.push.apply 2
var a = z.slice(0),
    b = y.slice(0);

Array.prototype.push.apply(a, b);
ready

Revisions

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