Array method solves the problem of JS string connection performance controversial _javascript techniques

Source: Internet
Author: User
1, traditionally, string concatenation has been one of the least performance operations in JS.
var text= "Hello";
text+= "world!";
This operation was not optimized by the earlier browsers.
Because the string is immutable, this means that you want to create an intermediate string to store the results of the connection. Frequently creating and destroying strings in the background is unusually low in performance.
2. After discovering this, developers are using array objects for optimization.
var buffer=[],i=0;
Buffer[i++]= "Hello"; adding elements by corresponding index values is faster than the push method
Buffer[i++]= "world!";
var text=buffer.join ("");
In earlier browsers, the intermediate strings were not created and destroyed, and in the case of large string concatenation, this technique had proved far faster than the use of additive methods.
3, now the browser of the string optimization has changed the situation of strings connected.
Safari, Opera, Chrome, Firefox, and IE8 all show better performance with the addition operator. However, the IE8 version was not optimized, so the array method is still valid.
This does not mean that we have to do browser detection when strings are connected. The size and number of strings are considered when deciding how to connect.
1 when the strings are relatively small (<20 characters) and the number of connections is also (<1000), all browsers use the addition operator to easily complete the connection in less than 1 hao seconds.
2 increase the number of strings or the size of the IE7 performance will be significantly reduced.
3 When the size of the string increases, the addition operator and number composition techniques in Firefox will have a smaller performance difference.
4 when the number of strings increases, the performance difference between the addition operators and the tricks in safari becomes smaller.
5 when changing the number or size of strings, the addition operator in both Chrome and opera keeps the leading edge.
Therefore, because the performance is inconsistent in the browser, the choice of technology depends on the actual situation and the face of the browser.
In most cases, the addition operator is preferred, and if the user is primarily using IE6 or 7, and the string size is larger or more numerous, then array technology is well worth it.

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.