Array .concat() vs. .push()

JavaScript performance comparison

Revision 17 of this test case created by

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
.push() (with empty array of length == 10)
var arr = new Array(10),
    toAdd = new Array(10);

for (var i = 0, iM = toAdd.length; i < iM; i++) {
 arr.push(toAdd[i]);
}
pending…
.push() (array of numbers)
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    toAdd = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (var i = 0, iM = toAdd.length; i < iM; i++) {
 arr.push(toAdd[i]);
}
pending…
concat strings, then split
var arr = "1 2 3 4 5 6 7 8 9 10",
    toAdd = "1 2 3 4 5 6 7 8 9 10";

var newstr = arr + " " + toAdd;
newstr.split(" ");

pending…
.concat() (array of numbers)
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    toAdd = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

arr = arr.concat(toAdd);
pending…
.concat() (several arrays of numbers)
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    toAdd = [[1, 2], [3], [4], [5], [6], [7], [8], [9], [10]];


for (var i = 0, iM = toAdd.length; i < iM; i++) {
 arr = arr.concat(toAdd[i]);
}
pending…
.push() w/ .apply()
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    toAdd = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

Array.prototype.push.apply(arr, toAdd);
pending…
push apply alt
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    toAdd = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

arr.push.apply(arr, toAdd);
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