JavaScript Learning Notes Two string stitching _ basics

Source: Internet
Author: User
var str= "Hello";
str+= "World";
In fact, the steps that this code performs behind the scenes are as follows:
(1) Create a string that stores "Hello".
(2) Create a string that stores "world".
(3) Create a string that stores the result of the connection.
(4) Copy the current contents of STR to the results.
(5) Copy "World" to the result.
(6) Update Str so that it points to the result.
Each completion of a string connection performs steps 2 through 6, making this operation very resource-intensive. If you repeat this process hundreds of times, or even thousands of times, it can cause performance problems. The workaround is to store the string with the array object and then create the last string with the Join () method (the argument is an empty string). Imagine replacing the previous code with the following code:
Copy Code code as follows:

var str=new Array ();
str[0]= "Hello";
Str[1]= "World";
Str.join ("");

In this way, no matter how many strings are introduced into the array, it is not a problem because the join operation occurs only when the join () method is invoked. At this point, the following steps are performed:
(1) Create a string that stores the results.
(2) Copy each string to the appropriate location in the result.
Copy Code code as follows:

function StringBuilder () {
This._string=new Array ();
}
Stringbuilder.prototype.append=function (str) {
This._string.push (str);
}
Stringbuilder.prototype.tostring=function () {
Return This._string.join ("");
}

Related Promotion efficiency articles:
The fastest way to stitch an HTML array string

The StringBuffer class of the concatenation of the large string of JavaScript

Learn more about the previous articles in the cloud-dwelling community.

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.