JS array append the Array Using the pitfall of push. apply

Source: Internet
Author: User

No ready-made functions are available for appending arrays to JS arrays. I have been used to a for so many years. push. apply (a, B); this kind of self-thinks cool, does not need to write the for loop, and has never encountered any problems, it was not until today that I want to append B to be a large array. A = new Array (); B = new Array (125624);. push. apply (a, B); the above Code throws the following exception in chrome mac: Uncaught RangeError: Maximum call stack size exceeded if you change the Array to B = new Array (125623 ); A small element is enough. I tested the problem that other browsers have large arrays, but the critical values of different browsers vary. Searching 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: 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 in large arrays, but also to consider forEach from the performance perspective is the fastest

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.