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.