Array .concat() vs. .push() (v8)

Revision 8 of this benchmark created on


Description

Compare .concat(), .push(), and .push.apply()

Test runner

Ready to run.

Testing in
TestOps/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]);
}
ready
.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]);
}
ready
.concat() (with empty array of length == 10)
var arr = new Array(10),
    toAdd = new Array(10);

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

Revisions

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