Use stringbuilder in Javascript

Source: Internet
Author: User

Introduction:
Some major operations, especially in the client browser, were considered very difficult not long ago. Nowadays, various development applications made by popular Ajax have quickly become a popular technology. However, most browsers on the market do not handle string connections very quickly.
The good news is that although ie processes string connections slowly, character operations are already the fastest. For this reason, I decided to write a simple stringbuilder class to save the independent string to an array and then use the join method to perform the join output operation. In the test, I used 5000 strings, 117 times faster than the equivalent operation S1 + = S2. After adding 10000 strings, It is faster than 261 times.

// Introduce a new stringbuilder Class Object
// Add the provided value
Function stringbuilder (value)
{
This. Strings = new array ("");
This. append (value );
}

// Assign a value to the specified value after the object
Stringbuilder. Prototype. append = function (value)
{
If (value)
{
This. Strings. Push (value );
}
}

// Clear Character Buffer
Stringbuilder. Prototype. Clear = function ()
{
This. Strings. Length = 1;
}

// Convert objects with strings
Stringbuilder. Prototype. tostring = function ()
{
Return this. Strings. Join ("");
}

The code is so simple and straightforward that it shocould be self-explanatory. Now here's an example of how to use it:
The code looks simple and straightforward. Self-explanatory, the following describes how to use this class

// Create a stringbuilder object
VaR sb = new stringbuilder ();

// Assign some strings
SB. append ("lorem ipsum dolor sit Amet, consectetuer adipiscing elit ,");
SB. append ("sed diem nonummy nibh euismod tincidunt ut lacreet dolore ");
SB. append ("Magna aliguam erat volutpat .");

// Obtain all character values
VaR S = sb. tostring ();

It is still very simple and straightforward. If you use stringbuilder in. net, you will also know how to use it here.
Compare edge to edge in HTML. You can use it to design some tests to see what is different. On my machine, it takes 12 seconds to connect 5000 characters. Stringbuilder takes only 110 milliseconds. 10000 strings, which occupy all the IE clock. Stringbuilder only takes 230 milliseconds. Each second is reduced by 1/4 seconds, which is a significant increase.

 

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.