Performance of javascript string connection (multiple browsers)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.