Test included in the book Code As follows:
Copy code The Code is as follows: <HTML>
<Head>
<Title> example </title>
</Head>
<Body>
<P> <strong> Note: </strong> the latest versions of Firefox seem to have fixed the String concatenation problem. if you are using Firefox 1.0 or later, the string buffer may be actually take longer than normal String concatenation. </P>
<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>
</Html>
The execution results in Firefox/3.0.3 are as follows:
Concatenation with plus: 5 milliseconds
Concatenation with stringbuffer: 10 milliseconds
The execution result in IE6 is as follows:
Concatenation with plus: 234 milliseconds
Concatenation with stringbuffer: 62 milliseconds
1. The performance of the two methods varies greatly.
2. It seems that IE6's string connection processing capability is worse than ff3.
3. The results of IE6 and ff3 are the opposite. It seems that you will pay attention to the browser when optimizing the write connection in the future.