Discuss the performance of js String Array splicing, js String Array Splicing

Source: Internet
Author: User

Discuss the performance of js String Array splicing, js String Array Splicing

We know that in js, string connection is one of the operations with the lowest performance.
For example:

Copy codeThe Code is as follows:
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.

Therefore, array objects can be used for optimization.
For example:

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.
 
Today, 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.

When the string is relatively small (less than 20 characters) and the number of connections is small (less than 1000), all browsers can easily complete the connection in less than 1 millisecond using the addition operator. When you increase the number of strings or increase the size, the performance in IE7 will be significantly reduced. When the string size increases, the performance difference between the plus operators and the number composition techniques in Firefox will become smaller. When the number of strings increases, the performance difference between the algorithm operators and the number composition skills in Safari will decrease. Changing the number of strings or increasing the size of strings, the algorithm 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.
 
In general, if it is a semantic string, Array should not be used, for example:
'Hello, my name is '+ name;
 
If the string is "repeated in similar cases", we recommend that you use Array, for example:

var array = []; for (i = 0; i < length; i++) { array[i] = '<li>' + list[i] + '</li'>; } document.getElementById('somewhere').innerHTML = array.join('\n');

We will introduce the performance comparison of js String Array connections and hope to help you.


How to Use JS String concatenation Array

Here, temp is a JSON array. You can spell it out by code in the json array format.
 
JS String concatenation Array

This way
For (I = 0; I <title. length; I ++)
{
Temp [I] = {"title": title [I], "content": content [I], "point ": pointX [I] + "|" + pointY [I]};
}

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.