5,276,406 members and growing! (16,103 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

JavaScript StringBuilder

By K.Collins

An extremely simple, light-weight JavaScript StringBuilder class. Hundreds of times faster than string concatenation in IE.
Javascript, XML, HTML, Windows, Visual Studio, Ajax, Dev

Posted: 6 Dec 2005
Updated: 6 Dec 2005
Views: 38,404
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
33 votes for this Article.
Popularity: 7.21 Rating: 4.75 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 3.0%
3
4 votes, 12.1%
4
28 votes, 84.8%
5

Introduction

Once upon a time, not so long ago, pushing any significant processing to the client browser was considered a bad practice. Now with the rise in popularity of AJAX style development, it has suddenly become a hot new technology. Unfortunately, the most commonly used browser on the market is painfully slow at one of the most common tasks in programming -- string concatenation.

The good news is that although IE is slow when it comes to string concatenation, it is quite fast with array operations. With this in mind, I decided to write a simple StringBuilder class that pushes individual strings into an array and then uses the join method to produce the concatenated output string. In tests I have run with 5,000 strings, it is 117 times faster than the equivalent string concatenation using the s1 += s2 syntax. With 10,000 strings, it is an amazing 261 times faster!

Using the code

The StringBuilder class only provides four methods: a constructor, an append method, a clear method, and a toString method. You can add more properties and methods if you need them, but I chose to keep it as simple as possible for this article.

Here is the entire script that defines the StringBuilder class:

// Initializes a new instance of the StringBuilder class

// and appends the given value if supplied

function StringBuilder(value)
{
    this.strings = new Array("");
    this.append(value);
}

// Appends the given value to the end of this instance.

StringBuilder.prototype.append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
}

// Clears the string buffer

StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}

// Converts this instance to a String.

StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}

The code is so simple and straightforward that it should be self-explanatory. Now here's an example of how to use it:

// create a StringBuilder

var sb = new StringBuilder();

// append some text

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.");

// get the full string value

var s = sb.toString();

Again, so simple and straightforward it shouldn't require any further explanation. If you've ever used the StringBuilder in .NET, then you already know how to use this one.

If you download the demo project, you will find an HTML page that performs a side-by-side comparison of StringBuilder vs. string concatenation. You can use it to run your own tests to see the difference for yourself. On my machine, it takes IE over 14 seconds to concatenate 5,000 strings. The StringBuilder does it in 110 ms. With 10,000 strings, it takes IE a full minute to concatenate. The StringBuilder does it in 230 ms. More than a minute to less than a quarter of a second, that's a fairly significant improvement!

Conclusion

The whole purpose of pushing processing to the client is to provide a richer, more responsive user experience. That means, it's important to make sure the client-side code is as efficient as possible. I hope this article helps your client-side string building code really scream.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

K.Collins


IT professional with ten years experience designing and building software and web applications for local, national, and global organizations in a variety of industries including telecommunications, insurance, accounting, finance, not-for-profit, marketing, payment services, e-commerce, and e-business.
Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThank you.memberarockj14:36 11 Jun '08  
GeneralRe: Thank you.memberK.Collins6:53 12 Jun '08  
GeneralEven Faster...memberLTSpeed4:46 21 Jun '07  
GeneralThanksmemberSuperDuckZA_PS5:17 8 Apr '07  
GeneralRe: ThanksmemberK.Collins5:34 9 Apr '07  
GeneralVery Good idea, with a small change!memberAjaxDino15:50 18 Jan '07  
GeneralRe: Very Good idea, with a small change!memberK.Collins5:52 19 Jan '07  
GeneralDoesn't work in Firefox?memberjswoofer17:55 17 Sep '06  
GeneralRe: Doesn't work in Firefox?memberAndrei Bozantan8:36 22 Sep '06  
GeneralIs it from Atlas?memberabakar18:32 12 Dec '05  
GeneralRe: Is it from Atlas?memberK.Collins5:00 13 Dec '05  
GeneralRe: Is it from Atlas?memberabakar18:27 13 Dec '05  
GeneralCross Browser CompatibiltymemberDavy Boy4:39 12 Dec '05  
GeneralSuggestionmemberJuergen Posny0:16 8 Dec '05  
GeneralRe: Suggestionmembershaul_ahuva2:54 8 Dec '05  
GeneralRe: SuggestionmemberJuergen Posny4:04 8 Dec '05  
GeneralRe: SuggestionmemberK.Collins7:21 8 Dec '05  
GeneralGood ideamembershaul_ahuva9:36 7 Dec '05  
GeneralBad Linksmemberfwsouthern16:03 6 Dec '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Dec 2005
Editor: Smitha Vijayan
Copyright 2005 by K.Collins
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project