JavaScript Learning Notes (vii) string concatenation _javascript Tips

Source: Internet
Author: User
Connection of Strings
One, the most commonly used =
Always said that this way of efficiency is the lowest, why? You can look at the real process of this approach.
var str = "Hello";
str = "World";
(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. All the time to discard this usage, hahaha. ^_^
second, join () method
Copy Code code as follows:

Button call
function Joinfn () {
var arr = new Array;
Arr[0] = "John";
ARR[1] = "Dick";
Alert (Arr.join (""));
}

The following steps are performed:
(1) Create a string that stores the results.
(2) Copy each string to the appropriate location in the result.
The efficiency of this method is faster than that of the first.
third, encapsulate a custom class
There are no StringBuilder classes in JavaScript like C #, but we can customize a StringBuilder class, and the method of building a class is the "mixed constructor/prototype approach" mentioned in the previous article.
Copy Code code as follows:

Customize a StringBuilder class to connect strings
function StringBuilder () {
This._strings = new Array ();
}
StringBuilder.prototype.append = function (str) {
This._strings.push (str);
};
StringBuilder.prototype.toString = function () {
Return This._strings.join ("");
};
Button call
function Myconnectclassfn () {
var sb = new StringBuilder ();
Sb.append ("John");
Sb.append ("Dick");
var strresult = sb.tostring ();
alert (strresult);
}

Summarize
Finally compared to these three methods, the book said the last one of the fastest, but after my test, looks like join () is the fastest, the third is the slowest, my custom StringBuilder class has a problem?
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.