Skip to content
November 9, 2009 / Thomas Danemar

JavaScript Stringbuilder, Because Concatenating With += Won’t Cut It

This is my StringBuilder. There are many like it, but this one is mine.

Ahem. Oh yes, StringBuilder. Why would you need a tool to help you build strings? Because concatenating a large amount of strings using += is pretty inefficient. Pushing the strings onto an array and then joining the array together at the end is a lot faster.
For an interesting analysis of the differences in performance, check out this post by by Josh Powell.

To the point. To faciliate adding to and joining arrays of strings, I made a version of the .NET StringBuilder class for JavaScript. Here it is:

var tools = {
	getStringBuilder: function() {
		var data = [];
		var counter = 0;
		return {
			// adds string s to the stringbuilder
			append: function(s) { data[counter++] = s; return this; },
			// removes j elements starting at i, or 1 if j is omitted
			remove: function(i, j) { data.splice(i, j || 1); return this; },
			// inserts string s at i
			insert: function(i, s) { data.splice(i, 0, s); return this; }
			// builds the string
			toString: function(s) { return data.join(s || ""); return this; },
        	};
    	}
};

The object “tools”, as your probably understand, is where I like to keep most of my utility functions like this one. Using it is fairly straight forward:

var sb = tools.getStringBuilder();
sb.append("Hi");
sb.append("there");
sb.append("buddy");
var greeting = sb.toString();

Or if you’re really a fan of the jQuery style of method chaining:

var greeting = tools.getStringBuilder()
   .append("Hi")
   .append("there")
   .append("buddy")
   .toString();

And that’s it!

(Yes, I’m aware I’m not the first one to write a StringBuilder for JavaScript. But I like this one so I’ll blog about it anyway.)

Advertisements

2 Comments

Leave a Comment
  1. Craig HIlles / Feb 3 2013 17:17

    I ran a test under IE 8, chrome, and firefox, appending to a string 1,000,000 times, and this class is slower than just regular concatenation . You can try it here: http://www.mybtree.com/sbtest.htm

    Yours is “tools stringbuilder”.

    I’ve been trying to find a stringbuilder class for javascript that runs faster than just plain string concatenateion and have failed so far…

    • Thomas Danemar / Feb 4 2013 23:29

      Yes, this post is a bit dated. All modern browsers, including IE from version 8, have string optimizations that make regular concatenation faster than array joins. There’s no reason to use a stringbuilder anymore, unless you’re targeting older versions of IE.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: