Array.prototype.push.apply vs concat

JavaScript performance comparison

Revision 20 of this test case created by tenbits

Preparation code

<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;
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
concat
var a = x.slice(0),
    b = y.slice(0);

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

Array.prototype.push.apply(a, b);
pending…
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]);
}
pending…
Cached Array#push
var a = x.slice(0),
    b = y.slice(0);

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

while (c--) {
  a.push(b[c]);
}
pending…
var a = x.slice(0),
    b = y.slice(0),
    bl = b.length,
    al = a.length,
    i = 0;
  
while (i < bl) {
  a[al++] = b[i++];
}
pending…

Compare results of other browsers

Revisions

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

0 Comments