Comparison of performance between 4 methods that can add items to an array:
Add using indexer
The code copy is as follows:
console.time("index");
var a = [];
for (var i = 0, l = times; i < l; i++) {
a[i] = i;
}
console.timeEnd("index");
Use push method
The code copy is as follows:
console.time("push");
var a = [];
for (var i = 0, l = times; i < l; i++) {
a.push(i);
}
console.timeEnd("push");
Use the concat method
The code copy is as follows:
console.time("concat");
var a = [];
for (var i = 0, l = times; i < l; i++) {
a.concat(i);
}
console.timeEnd("concat");
Use the concat method, the parameters are array
The code copy is as follows:
console.time("concat with array");
var a = [];
for (var i = 0, l = times; i < l; i++) {
a.concat([i]);
}
console.timeEnd("concat with array");
Set times to 100 million times:
The code copy is as follows:
index: 0.310ms
push: 1.476ms
concat: 8.911ms
concat with array: 2.261ms
Set times to 100,000 (100,000) times:
The code copy is as follows:
index: 1.967ms
push: 11.980ms
concat: 70.410ms
concat with array: 28.292ms
Set times to 1000000 (million) times:
The code copy is as follows:
index: 138.559ms
push: 93.074ms
concat: 608.768ms
concat with array: 243.371ms
Set times to 100,000 (10 million) times:
The code copy is as follows:
index: 1473.733ms
push: 611.636ms
concat: 6058.528ms
concat with array: 2431.689ms
Summarize
This conclusion is only useful with Chrome browser
The execution efficiency of the concat method is the slowest
Compared with the argument transmission of the two concat methods, when the accepted parameters are arrays, the execution efficiency is higher than that of the accepted parameters as non-arrays.
In most cases, the execution efficiency of the indexer is higher than that of the push method.
When the number of executions increases, the execution efficiency of the indexer begins to be worse than that of the push method
Browser comparison
Thanks to netizens for pointing out that I lack experience, so here are the horizontal comparisons between browsers
First, use the concat method. In ie and firefox, the parameters are arrays, and the execution efficiency is slower than the other parameters are non-arrays, but the difference is not large.
Then it is certain that the index and push methods are faster than concat. Using the index method in ie is always faster than push. Push is slightly better in firefox but the difference is not big
Compared with the execution efficiency of index and push methods between the three browsers, the execution efficiency of firefox is much higher than that of ie and chrome. In a million times, it is basically 10 times faster. I'm the slowest compared to the other two.
The following are the results of millions:
The code copy is as follows:
// firefox
index: timer started
index: 229.79ms
push: timer started
push: 205.12ms
concat: timer started
concat: 2136.99ms
concat with array: timer started
concat with array: 2365.18ms
```
The code copy is as follows:
// ie
index: 2,533.744 milliseconds
push: 3,865.979 milliseconds
concat: 4,303.139 milliseconds
concat with array: 4,792.208 milliseconds
This article is just to explore the performance of JS. It will deepen your friends' understanding of JavaScript through comparison. I hope you can like it.