# String concatenation Efficiency

Source: Internet
Author: User
During script development, a large string is often combined and output according to a specific rule. For example, when writing a script control, you can control the HTML Tag output of the entire control's appearance. For example, when Ajax obtains the return value from the server and dynamically analyzes and creates HTML tags, however, I will not discuss the specific application of Concatenated strings here. I just want to discuss the splicing efficiency here.

String concatenation uses the "+ =" operator S + = string when writing code. This is the most well-known method. I wonder if you have noticed it, when the combined string capacity is dozens or even hundreds of K, the script execution is slow and the CPU usage is high. For example:

VaR STR = "01234567891123456789212345678931234567894123456789 ";
STR + = "51234567896123456789712345678981234567899123456789 \ n ";
VaR result = "";
For (VAR I = 0; I <2000; I ++) Result + = STR;

In this step, the resulting string is 200 kb and the time consumed is 1.1 seconds (this is related to the computer configuration). The peak CPU usage is 100%. (In order to see the effect more intuitively, I made more cycles ). It can be imagined that this step consumes more than one second, and the execution time of the entire script block will be unbearable, coupled with the time consumed by other code. Is there any optimization solution? Are there other methods? The answer is yes, or I will write this article as nonsense.

The faster way is to use arrays. During loop concatenation, strings are not concatenated into a string, but put into an array, and finally an array is used. join ("") to obtain the result string. Sample Code:

VaR STR = "01234567891123456789212345678931234567894123456789 ";
STR + = "51234567896123456789712345678981234567899123456789 \ n ";
VaR result = "", A = new array ();
For (VAR I = 0; I <2000; I ++) A [I] = STR;
Result = A. Join (""); A = NULL;

You can test and test the time consumed by combining a string of the same size. The test result here is: <15 ms. Note that the unit is milliseconds, that is to say, to combine such a K string, the time consumption of the two modes is about two orders of magnitude. What does this mean? This means that the latter has come back from work and is still working hard. I wrote a test page. You can copy the following code and save it as an HTM file. Open the page and test the efficiency of the two, I tested what the former did in half a minute, and the latter completed it in 0.07 seconds (10000 cycles ).

<Body>
String concatenation Times <input id = "totle" value = "1000" size = "5" maxlength = "5">
<Input type = "button" value = "String concatenation" onclick = "Method1 ()">
<Input type = "button" value = "array assignment join method" onclick = "method2 ()"> <br>
<Div id = "Method1"> & nbsp; </div>
<Div id = "method2"> & nbsp; </div>
<Textarea id = "show" style = "width: 100%; Height: 400"> </textarea>
<Script language = "JavaScript">
<! --
// The length of the concatenated string is 100 bytes Author: meizz
VaR STR = "01234567891123456789212345678931234567894123456789 ";
STR + = "51234567896123456789712345678981234567899123456789 \ n ";

// Method 1
Function Method1 ()
{
VaR result = "";
VaR totle = parseint (document. getelementbyid ("totle"). value );
VaR n = new date (). gettime ();

For (VAR I = 0; I <totle; I ++)
{
Result + = STR;
}

Document. getelementbyid ("show"). value = result;
VaR S = "String concatenation method: large string length after splicing" + result. Length + "byte," +
"Splicing time consumption" + (new date (). gettime ()-N) + "millisecond! ";
Document. getelementbyid ("Method1"). innerhtml = s;
}

// Method 2
Function method2 ()
{
VaR result = "";
VaR totle = parseint (document. getelementbyid ("totle"). value );
VaR n = new date (). gettime ();

VaR A = new array ();
For (VAR I = 0; I <totle; I ++)
{
A [I] = STR;
}
Result = A. Join (""); A = NULL;

Document. getelementbyid ("show"). value = result;
VaR S = "array assignment join method: large string length after splicing" + result. Length + "byte," +
"Splicing time consumption" + (new date (). gettime ()-N) + "millisecond! ";
Document. getelementbyid ("method2"). innerhtml = s;
}
// -->
</SCRIPT>

In the last few words, will all strings be joined using arrays after concatenation? This depends on your actual needs. There is no need to use the array method for the combination of several or K-level bytes, because opening the array variable is also consumable. If there is a combination of strings of several K or more, the array efficiency is high.

Bytes ------------------------------------------------------------------------------------------

Internet Explorer 6.0:
String concatenation: the size of the spliced large string is 1010000 bytes long and takes 22089 milliseconds!
Array value assignment join: the size of the concatenated string is 1010000 bytes long, And the concatenation time is 218 milliseconds!

Firefox 1.0:
String concatenation: the size of the spliced large string is 1010000 bytes long and takes 1044 milliseconds!
Array value assignment join: the size of the concatenated string is 1010000 bytes long, And the concatenation time is 1044 milliseconds!

Mozilla 1.7:
String concatenation: the size of the spliced large string is 1010000 bytes long and takes 1045 milliseconds!
Array value assignment join: the size of the concatenated string is 1010000 bytes long, And the concatenation time is 1044 milliseconds!

Netscape 7.0:
String concatenation: the size of the spliced large string is 1010000 bytes long and takes 10273 milliseconds!
Array value assignment join: the size of the concatenated string is 1010000 bytes long, And the concatenation time is 1138 milliseconds!

Opera 7.54:
String concatenation: the size of the spliced large string is 1010000 bytes long and takes 6968 milliseconds!
Array value assignment join: the size of the concatenated string is 1010000 bytes long, And the concatenation time is 6922 milliseconds!

The testing results of the loop 10000 times show that the efficiency can be greatly improved in IE and Netscape, while the two methods in Firefox Mozilla opera consume almost the same time, these data are sufficient to determine that the array join method is better than the traditional String concatenation method.

From: http://blog.csdn.net/meizz/archive/2005/12/14/552260.aspx

Related Keywords:

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.