JS array append array using push.apply problem
- 2020-03-30 03:14:49
- OfStack
a = new Array();
b = new Array(125624);
a.push.apply(a, b);
The above code throws the following exception under MAC chrome
Uncaught RangeError: Maximum call stack size exceeded
If I change the Array to b = new Array(125623); A smaller element is fine. Test the problem that every other browser has a large array to go wrong, but the threshold varies from browser to browser.
Search the http://stackoverflow.com/questions/1374126/how-to-append-an-array-to-an-existing-javascript-array/17368101#17368101 Other people have encountered such pits:
Array.prototype.extend = function (other_array) {
other_array.forEach(function(v) {this.push(v)}, this);
}
The advice given is that honest and practical forEach not only avoids the problem of large array exceptions, but also forEach is the fastest from a performance perspective
< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201406/2014060912043219.png ">
This pit gives me two thoughts:
A. ush. Apply (a, b); Or just for the interview questions to force, actual combat or more honest way that exception and performance, such as a small number of such as the dozens of nodes of the 3 d network topology play spring example layout is no problem, meet a large amount of data, such as this in HT for this 3 d large amount of data of Web performance examples to test out the problem.
2, http://stackoverflow.com/questions/1374126 Don't look on stackoverflow for the most votes. The truth is often in the hands of a few people. The 259-vote answer below is a pit.
< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201406/2014060912043220.png ">
< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201406/2014060912043221.png ">