Array Method to Solve JS string connection performance issues controversial

Source: Internet
Author: User

1. Traditionally, string connection has always been one of the operations with the lowest performance in js.
Var text = "Hello ";
Text + = "World! ";
Early browsers did not optimize this operation.
Because the string is immutable, this means that you need to create an intermediate string to store the connection results. Frequent background creation and destruction of string-based export performance is exceptionally low.
2. After discovering this, developers can use array objects for optimization.
Var buffer = [], I = 0;
Buffer [I ++] = "Hello"; // adding elements through the corresponding index value is faster than the push Method
Buffer [I ++] = "World! ";
Var text = buffer. join ("");
In earlier browsers, no intermediate string was created or destroyed. In the case of a large number of string connections, this technology has been proven to be much faster than the addition method.
3. Now, the browser's string optimization has changed the string connection situation.
Safari, Opera, Chrome, Firefox, and IE8 both use addition operators to deliver better performance. However, versions earlier than IE8 are not optimized, so the array method is still valid.
This does not mean that browser detection is required when strings are connected. When deciding how to connect, consider the size and quantity of strings.
1) when the string is relatively small (<20 characters) and the number of connections is also small (<1000 characters ), all browsers can easily complete the connection in less than 1 second using the addition operator.
2) When the number or size of strings is increased, the performance of IE7 will decrease significantly.
3) when the string size increases, the performance difference between the plus operators and the number composition skills in Firefox will decrease.
4) when the number of strings increases, the performance difference between the algorithm operators and the number composition skills in Safari will decrease.
5) change the number of strings or increase the number of hours, and the method operators in Chrome and Opera have always maintained a leading edge.
Therefore, because the performance of different browsers is inconsistent, the selection of technology depends on the actual situation and the browser.
In most cases, the addition operator is the first choice. If you mainly use IE6 or 7, and the string size is large or a large number, the array technology is worth it.

Related Article

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.