On the problem of using push.apply to append array of JS array _javascript skills

Source: Internet
Author: User

Copy Code code as follows:

A = new Array ();
b = new Array (125624);
A.push.apply (A, b);


The above code throws the following exception under the Mac Chrome

Copy Code code as follows:

uncaught rangeerror:maximum Call stack size exceeded


If you change the array to B = new Array (125623), small one element is good, test the other browsers also have a large array of errors, but different browser thresholds vary.

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

Copy Code code as follows:

Array.prototype.extend = function (Other_array) {
/* You should include a test to check whether Other_array really are an array */
Other_array.foreach (function (v) {This.push (v)}, this);
}


The advice given is to be honest with foreach, not only to avoid the exception of large arrays, but also to consider foreach as the fastest in terms of performance

This little pit gave me two points to think about:

1. Some fancy usages such as a.push.apply (A, b) or for the face of the test to install force on the line, or more to take the honest route to avoid encountering anomalies and performance of the pits, such as the small number of such as this dozens of nodes of the 3D network topology Spring layout example to play is not a problem, This 3D large data performance example of a really large amount of data, such as the HT for Web in this article, can test the problem.

2, http://stackoverflow.com/questions/1374126 from StackOverflow to find answers when not only stare at the most votes, the truth is often in the hands of a few people, the following figure 259 votes answer is a pit, 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.