Array .concat() vs. .push() (v13)

Revision 13 of this benchmark created by Don Willis on


Description

test was buggy!

you need to create the array separately for each test to have real results as before you were mutating the same array and thus with each successive test iteration you were testing array which was bigger and bigger.

and since concat test was run after the push it was tested on even larger arrays then push making it work SUPER slow


13: Added test case where concat is used to add small sections of the original array.

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 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(" ");

 
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
.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]);
}
ready

Revisions

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