The code copy is as follows:
a = new Array();
b = new Array(125624);
a.push.apply(a, b);
The above code throws the following exception under Mac Chrome
The code copy is as follows:
Uncaught RangeError: Maximum call stack size exceeded
If you change the array to b = new Array(125623); it would be great if you have a smaller element. I tested that other browsers also have problems with large arrays, but the critical values of different browsers are still different.
I searched http://stackoverflow.com/questions/1374126/how-to-append-an-array-to-an-existing-javascript-array/17368101#17368101 and found that someone encountered such a pitfall:
The code copy is as follows:
Array.prototype.extend = function (other_array) {
/* you should include a test to check whether other_array really is an array */
other_array.forEach(function(v) {this.push(v)}, this);
}
The suggestions given are to be honest and practical forEach, which can not only avoid the exception problems of large arrays, but also consider the performance of forEach as the fastest
This little pit gave me two thoughts:
1. Some fancy usages such as a.push.apply(a, b); it is enough to be used to show off when interview questions. In practice, it is better to take the honest route to avoid encountering abnormalities and performance pitfalls. For example, a small number of 3D network topology spring layout examples with dozens of nodes in this article are fine. Only when you encounter real big data volumes, such as HT for Web performance examples in this article, can you test the problem.
2. http://stackoverflow.com/questions/1374126 When looking for answers from stackoverflow, don’t just focus on the ones that vote the most. The truth is often in the hands of a few people. The answer with 259 votes in the figure below is a pit, and 34 votes are the most perfect analysis: