# push()
is faster than concat()
# conclusion
OK, briefly, I recommend you to use Array.prototype.push(arr1, arr2)
instead of arr1.concat(arr2)
when merge arrays. And you can take ES6 to simplify the push
methods: arr1.push(...arr2)
.
# How is it happens
# concat()
naive implementation
// create a new array
var arr3 = []
for(var i = 0;i < arr1.length; i++){
arr3[i] = arr1[i]
}
for(var i = 0;i < arr2.length; i++){
arr3[arr1.length + i] = arr2[i]
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# push()
naive implementation
for (var i = 0; i<arr2.length; i++){
arr1[arr1.length + i] = arr2[i]
}
1
2
3
2
3
As we can see, push()
just has less for
implementation than concat()
which makes it faster.