Performance issues with JavaScript string concatenation (multi-browser) _javascript tips

Source: Internet
Author: User
Tags stringbuffer javascript string concatenation
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
Related Article

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.