JS array append array with push.apply pit (turn)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.