Today, I suddenly remembered how to test the stringbuilder object efficiency under Js. So I wrote a loop to test it. The result was a bit unexpected.
1. directly use the + = Operator for 50 thousand cycles:
1 var sbtest // = new SYS. stringbuilder ();
2 For (VAR I = 0; I <50000; I ++)
3 {
4 var now = new date ();
5 sbtest + = now. getseconds () + "<br/> ";
6}
7
8 $ get ("message"). innerhtml = sbtest; //. tostring ('| <br/> ');
The test results are as follows:
32 s for IE and 2 S for Firefox during Loop
2. Use stringbuilder: var sbtest = new SYS. stringbuilder ();
For (VAR I = 0; I <50000; I ++)
{
VaR now = new date ();
Sbtest. append (now. getseconds ());
}
$ Get ("message"). innerhtml = sbtest. tostring ('| <br/> ');
The test results are as follows:
6 seconds for IE and 3 seconds for Firefox
The results differ so much. In ie, the efficiency of stringbuilder is obviously higher than that of + =, but there is no big difference in Firefox whether stringbuilder is used or not.