Compared the performance between 4 ways to add items to an array:
Use indexer to add
Use push method
Use concat method
Use the concat method, the parameter is an array
Set times to 10,000 (ten thousand) times:
Set times to 100000 (one hundred thousand) times:
Set times to 1000000 (millions) times:
Set times to 10000000 (ten million) times:
Summary
This conclusion is only applicable to Chrome browser
The execution efficiency of the concat method is the slowest
Compared with the parameter passing of the two concat methods, when the parameters are accepted as arrays, the execution efficiency is higher than when the parameters are accepted 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 inferior to the push method
Browser comparison
Thanks to the netizen for pointing out that I lack experience, so I will add a horizontal comparison between browsers here
The first is to use the concat method. In IE and Firefox, if the parameter is an array, the execution efficiency is slower than if the parameter is a non-array, but the difference is not big
Then the index and push methods are definitely faster than concat. Using the index method in IE is always faster than push. In Firefox, push is slightly better but the difference is not big
Comparing the execution efficiency of the index and push methods between the three browsers, the difference is huge. The execution efficiency of Firefox is much higher than that of IE and Chrome. In the case of millions of times, it is basically 10 times faster. Compared with other browsers, the execution efficiency of Firefox is basically 10 times faster. The slowest of the two
The following are the results of millions of times:
This article only discusses the performance of JS, and deepens friends' understanding of javascript through comparison. I hope you will like it.