The test code included in the book is as follows
Copy Code code as follows:
<title>Example</title>
<body>
<p><strong>Note:</strong> the latest versions of Firefox seem to have fixed the string concatenation Pro Blem. If you are using Firefox 1.0 or later, the string buffer may actually take longer normal string than Gt
<script type= "Text/javascript" >
function StringBuffer () {
this.__strings__ = new Array;
}
StringBuffer.prototype.append = function (str) {
This.__strings__.push (str);
};
StringBuffer.prototype.toString = function () {
Return This.__strings__.join ("");
};
var D1 = new Date ();
var str = "";
for (Var i=0 i < 10000; i++) {
STR + + "text";
}
var d2 = new Date ();
document.write ("Concatenation with Plus:" + (D2.gettime ()-d1.gettime ()) + "milliseconds");
var buffer = new StringBuffer ();
D1 = new Date ();
for (Var i=0 i < 10000; i++) {
Buffer.append ("text");
}
var result = buffer.tostring ();
D2 = new Date ();
document.write ("<br/>concatenation with StringBuffer:" + (D2.gettime ()-d1.gettime ()) + "milliseconds");
</script>
</body>
The results of the execution in firefox/3.0.3 are as follows:
Concatenation with plus:5 milliseconds
Concatenation with stringbuffer:10 milliseconds
The results of the implementation in IE6 are as follows:
Concatenation with plus:234 milliseconds
Concatenation with stringbuffer:62 milliseconds
1. Two different ways of performance vary greatly
2. It seems that IE6 string connection processing ability is worse than FF3
3.ie6 and FF3 two ways to reverse the results, it seems that later write connection optimization and attention to the browser AH