JavaScript uses StringBuffer class to promote + + concatenation string efficiency _javascript tips

Source: Internet
Author: User
Tags stringbuffer
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
</body>
<script type= "Text/javascript" ><!--
var str = ' Hello ';
str = ' world ';
Steps 2 through 6 are performed each time a string connection is completed
In fact, the steps that this code performs behind the scenes are as follows:
/**//*
1. Create a string that stores ' hello '
2. Create a string that stores ' world '
3. Create a string that stores the results of a link
4. Copy the current contents of STR to the results
5. Copy ' World ' to results
6. Update Str so that it points to the result
*/

The best way to improve performance is to use array methods to stitch strings
Create a StringBuffer class
function StringBuffer () {
this.__strings__ = [];
};
StringBuffer.prototype.append = function (str) {
This.__strings__.push (str);
};
StringBuffer.prototype.toString = function () {
Return This.__strings__.join (");
};

Call the StringBuffer class to implement the concatenation string
Perform step 2 each time you complete a string connection
In fact, the steps that this code performs behind the scenes are as follows:
/**//*
1. Create a string that stores the results
2. Copy each string to the appropriate location in the result
*/
var buffer = new StringBuffer ();
Buffer.append (' Hello ');
Buffer.append (' World ');
var result = buffer.tostring ();

Use + + to save 50%~66% time with StringBuffer analogy
-->
</script>
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.