Question about using push. apply to append arrays to JS Arrays

Source: Internet
Author: User

Copy codeThe Code is as follows:
A = new Array ();
B = new Array (125624 );
A. push. apply (a, B );


The above code throws the following exception in chrome of mac:

Copy codeThe Code is as follows:
Uncaught RangeError: Maximum call stack size exceeded


If you change the Array to B = new Array (125623), the problem is that a small element is enough. After testing, other browsers also have a large Array, but the critical values of different browsers vary.

Search for the http://stackoverflow.com/questions/1374126/how-to-append-an-array-to-an-existing-javascript-array/17368101#17368101 found that someone encountered such a pitfall:

Copy codeThe Code is as follows:
Array. prototype. extend = function (other_array ){
/* You shoshould include a test to check whether other_array really is an array */
Other_array.forEach (function (v) {this. push (v)}, this );
}


The advice is to be honest and practical, not only to avoid exceptions of large arrays, but also to consider forEach from the performance perspective is the fastest

This trap gives me two points of thinking:

1. Some fancy usage such as. push. apply (a, B); or apply it to interview questions. In practice, we should take more honest steps to avoid exceptions and performance pitfalls, for example, there are a small number of 3D network topology spring la s for dozens of nodes, A real big data volume, such as the HT for Web 3D Big Data performance example in this article, can be used to test the problem.

2, http://stackoverflow.com/questions/1374126 from stackoverflow to find the answer should not just stare at the most votes, the truth is often in the hands of a few people, 259 votes of the answer is a pitfall, 34 votes is the most perfect analysis:

 

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.