Post the complete code first.
Copy Code code 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;//More than one row, you will consume more processing time
var i = "ASDFASDF";
}
StringBuffer.prototype.toString = function () {
This._strings.join ("");
}
/*string class + Number Cumulative * *
var d = new Date ()//Cumulative start time
var str = "1";
for (var i = 0; i < i++) {
STR + + "SSSS";
for (var i = 0; i < 30000; i++) {
STR + + "text";
}
}
var d2 = new Date (); Cumulative End Time
document.write ("+:" + (D2.gettime ()-d.gettime ()) + "milliseconds");//Add 30000
/* Custom StringBuffer class String Additive * *
D = new Date (); StringBuffer Start time
var buffer = new StringBuffer ();
for (var i = 0; i < 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");
/* Direct use of array without encapsulation * *
D = new Date ();
var arr = new Array ();
for (var i = 0; i < 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]
First of all, my machine configuration is shown in Figure 1:
。
Run time results as shown in Figure 2:
Running on an n-old machine results as shown in Figure 3:
The above stack of codes and graphs may be messed up. Summarized as follows:
Low machine configuration: "+" string splicing mode consumption time is significantly higher than array mode
High machine configuration: "+" string concatenation method consumption time may be the same as the array method. will be shorter.
Summary: For insurance purposes. It is also recommended that you use array for string concatenation operations.