JS Array Append array There is no ready-made function, so many years I have been accustomed to a.push.apply (a, b); this kind of self-thought is very cool, do not need to write for the loop of writing, has not encountered any problems, until today I want to append B is a very large array when I met the pit.
| 123 |
a = newArray(); b = newArray(125624); a.push.apply(a, b); |
The above code throws the following exception under Chrome for Mac
| 1 |
Uncaught RangeError: Maximum call stack size exceeded |
If you change the array to B = new Array (125623), the small element is actually good, test the other browsers also have a large array of errors, but different browser thresholds are also various.
Searched for http://stackoverflow.com/questions/1374126/how-to-append-an-array-to-an-existing-javascript-array/17368101# 17368101 found that some people also encountered such a pit:
| 1234 |
array.prototype.extend = function (Other_array) { /* you should include a test to check whether Other_array really are an arra Y */      other_ Array.foreach ( function (v) { this .push (v)}, this } |
The advice given is to honestly use foreach, not only to avoid the exception of large arrays, but also to consider foreach from a performance perspective as the fastest
This little pit gave me two thoughts:
1, some fancy usage, such as a.push.apply (A, b), or for the face of the question loading force on the line, the actual practice or more to take the honest route to avoid encountering anomalies and performance of the pit, such as the small number of this dozens of-node 3D network topology Spring layout example play is no problem, This 3D large data-volume performance example of the HT for Web in this article can test the problem by encountering real big data volumes.
2, http://stackoverflow.com/questions/1374126 from StackOverflow to find the answer not only stare at the most votes, the truth is often mastered in the hands of a few, 259 votes answer is a pit, 34 votes is the most perfect analysis:
JS array append array with push.apply pit (turn)