Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A simple array-backed string builder implementation for JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


If you build large strings in Javascript, you may want to consider using StringBuilder.

Typically you would build strings like this:

var myString = "";
for (var i = 0; i < 1000; i++) {
    myString += i.toString();

The problem with that is, it uses a lot of memory. A lot of memory. After the 3rd iteration the following values will be in memory:


Enter, the string builder:

var myStringBuilder = StringBuilder();
for (var i = 0; i < 1000; i++) {

After the 3rd iteration, only these values are in memory:


This is a very simple example, imagine looping through a 2d array to build an html table. Memory balloons very quickly which leads to excessive Garbage Collection and poor performance using the concatenation method.

StringBuilder can also be used as an AMD module with Require.js:

define(['stringbuilder'], function(StringBuilder) {
    var sb = StringBuilder();
    sb.toString(); // "foobar"
Something went wrong with that request. Please try again.