Search code examples
javascriptstringbuilder

Does JavaScript have a built-in stringbuilder class?


I see a few The Code Project solutions.

But is there a regular implementation in JavaScript?


Solution

  • If you have to write code for Internet Explorer make sure you chose an implementation, which uses array joins. Concatenating strings with the + or += operator are extremely slow on IE. This is especially true for IE6. On modern browsers += is usually just as fast as array joins.

    When I have to do lots of string concatenations I usually fill an array and don't use a string builder class:

    var html = [];
    html.push(
      "<html>",
      "<body>",
      "bla bla bla",
      "</body>",
      "</html>"
    );
    return html.join("");
    

    Note that the push methods accepts multiple arguments.