This article describes how to add performance analysis items to the array by JS. For more information, see the performance comparison between the four methods that can add items to the array:
Add With Indexer
The Code is as follows:
Console. time ("index ");
Var a = [];
For (var I = 0, l = times; I <l; I ++ ){
A [I] = I;
}
Console. timeEnd ("index ");
Use the push Method
The Code 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 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 parameter is an array.
The Code 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 10000 () times:
The Code is as follows:
Index: 0.310 ms
Push: 1.476 ms
Concat: 8.911 ms
Concat with array: 2.261 ms
Set times to 100000 (100,000) times:
The Code is as follows:
Index: 1.967 ms
Push: 11.980 ms
Concat: 70.410 ms
Concat with array: 28.292 ms
Set times to 1000000 (million) times:
The Code is as follows:
Index: 138.559 ms
Push: 93.074 ms
Concat: 608.768 ms
Concat with array: 243.371 ms
Set times to 10000000 (10 million) times:
The Code is as follows:
Index: 1473.733 ms
Push: 611.636 ms
Concat: 6058.528 ms
Concat with array: 2431.689 ms
Summary
This conclusion is only applicable to the chrome browser.
Concat method execution efficiency is the slowest
Compared with the passing parameters of the two concat methods, when the accepted parameter is an array, the execution efficiency is higher than that of the accepted parameter.
In most cases, the execution efficiency of the indexer is higher than that of the push method.
As the number of executions increases, the efficiency of the indexer execution is not as high as that of the push method.
Browser comparison
Thanks to some netizens for pointing out that I am not experienced enough. Here I will add a horizontal comparison between browsers.
First, the concat method is used. In ie and firefox, the parameter is the array execution efficiency, but other parameters are not the array, but the difference is not great.
Then, the index and push methods are faster than concat. in ie, the index method is always faster than push. In firefox, the push method is slightly better, but the difference is not big.
Comparing the execution efficiency of the index and push methods between the three browsers is huge. The execution efficiency of firefox is much higher than that of ie and chrome. In the case of millions of times, basically 10 times faster, and ie is the slowest
The following are the results of millions of requests:
The Code is as follows:
// Firefox
Index: timer started
Index: 229.79 ms
Push: timer started
Push: 205.12 ms
Concat: timer started
Concat: 2136.99 ms
Concat with array: timer started
Concat with array: 2365.18 ms
"
The Code is as follows:
// Ie
Index: 2,533.744 Ms
Push: 3,865.979 Ms
Concat: 4,303.139 Ms
Concat with array: 4,792.208 Ms
This article only discusses the performance of javascript. Through comparison, I hope you will enjoy it.