The JS string accumulation Array is not necessarily faster than the string accumulation (depending on the computer configuration)

Source: Internet
Author: User

Paste the complete code first.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function StringBuffer (){
This. _ strings = new Array ();
}
StringBuffer. prototype. append = function (str ){
This. _ strings. push (str );
// StringBuffer. prototype. length = this. _ strings. length; // each row consumes more processing time.
Var I = "asdfasdf ";
}
StringBuffer. prototype. toString = function (){
This. _ strings. join ("");
}
/* String class + number accumulation */
Var d = new Date (); // accumulate the start time
Var str = "1 ";
For (var I = 0; I <200; I ++ ){
Str + = "ssss ";
For (var I = 0; I <30000; I ++ ){
Str + = "text ";
}
}
Var d2 = new Date (); // accumulate the end time
Document. write ("+:" + (d2.getTime ()-d. getTime () + "milliseconds"); // when the value is 30000
/* Add custom StringBuffer class strings */
D = new Date (); // StringBuffer Start Time
Var buffer = new StringBuffer ();
For (var I = 0; I <200; I ++ ){
Str + = "ssss ";
For (var I = 0; I <30000; I ++ ){
Buffer. append ("text ");
}
}
Var resultstr = buffer. toString ();
D2 = new Date ();
Document. write ("<br/> StringBuffer:" + (d2.getTime ()-d. getTime () + "milliseconds ");
/* Use Array directly without encapsulation */
D = new Date ();
Var arr = new Array ();
For (var I = 0; I <200; I ++ ){
Str + = "ssss ";
For (var I = 0; I <30000; I ++ ){
Arr. push ("text ");
}
}
Var resStr = arr. join ("");
D2 = new Date ();
Document. write ("<br/> Array:" + (d2.getTime ()-d. getTime () + "milliseconds ");
</Script>
[Code]

Let's talk about my machine configuration 1:
.

Running time result 2:

Run result 3 on an old N Host:

The above pile of code and figures may be messy. Summary:

When the machine configuration is low: the "+" String concatenation method consumes much more time than the Array method.
When the machine configuration is high: the time consumed by the "+" String concatenation method may be the same as that of the Array method. Even shorter.

Conclusion: To be safe. We recommend that you use Array to concatenate strings.

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.