String concatenation in javascript _ javascript skills

Source: Internet
Author: User
String concatenation is required by all programming languages. When splicing results are long, how to ensure efficiency becomes a very important issue. This article describes String concatenation in Javascript. I hope it will help you. Let's take a look. Recently, in the study of javascript advanced programming, there is a description of the character string. The original Article is roughly as follows: strings in ECMAScript are immutable, that is, once a string is created, their values cannot be changed. To change the saved string of a variable, first destroy the original string and then fill the variable with another string containing the new value. For example:

The Code is as follows:


Var lang = "Java ";
Lang = lang + "Script ";

The procedure is as follows: first create a new string that can contain 10 characters, and then fill the string with "Java" and "Script ", the last step is to destroy the original strings "Java" and "Script", because the two strings are useless. However, in earlier browsers (such as IE6), String concatenation speeds consume a lot of performance.

As a result, I think of Java. The string mechanism in Java is similar to that in js (that is, it cannot be changed after it is created. to change it, you can only destroy the original value ), however, Java has a StringBuffer that solves the problem of variable strings. js does not have a similar method. But we can simulate this buffer mechanism. The source code is as follows:

The Code is as follows:


Function StringBuffer (){
This. _ strings _ = new Array ();
}
StringBuffer. prototype. append = function (str ){
This. _ strings _. push (str );
Return this; // convenient for chained operations
}
StringBuffer. prototype. toString = function (){
Return this. _ strings _. join ("");
}

/* Test */
Var buffer = new StringBuffer ();
Buffer. append ("Hello"). append ("javascript ");

Var result = buffer. toString ();
Alert (result );

Ps: gist address: https://gist.github.com/hehongwei44/fe71f10e4d2d9295aeab

We have simulated the mechanism, but the performance of this method differs from that of String concatenation. We can test the test code as follows:

The Code is as follows:


Var d1 = new Date ();
Var str = "";
For (var I = 0; I <10000; I ++ ){
Str + = "text ";
}
Var d2 = new Date ();
Document. write ("Test 1 Cost:" + (d2.getTime ()-d1.getTime ()/1000 + "second" +"
");

Var oBuffer = new StringBuffer ();
D3 = new Date ();
For (var I = 0; I <10000; I ++ ){
OBuffer. append ("text ");
}
Var sResult = oBuffer. toString ();
D4 = new Date ();
Document. write ("Test 2 Cost:" + (d4.getTime ()-d3.getTime ()/1000 + "second ");

The test results are as follows: (the test results may vary depending on the environment ):

1. Make a comparison based on 1000 times. The execution of the two is very fast (basically several milliseconds), and the difference between the two will not exceed 10 milliseconds.
2. In the case of 10000 calls, the execution result is similar to the above, but the former has many calls under IE6.
3. When the string is spliced in IE6 with the base of 100000 times, it takes much more time. other browsers have little difference, but some are shorter than StringBuffer.

Conclusion

1. When the number of Concatenated words is less than 1000 times, we can use the former boldly. Generally, we rarely encounter thousands of Concatenated words.
2. Other browsers have no performance problems with splicing, mainly IE6. If splicing times are tens of thousands or 100,000, we recommend that you use StringBuffer to simulate IE6 separately.

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.