Does JavaScript have a built in stringbuilder class?

leora picture leora · Jan 18, 2010 · Viewed 155.8k times · Source

I see a few code project solutions.

But is there a regular implementation in JavaScript?

Answer

Fabian Jakobs picture Fabian Jakobs · Jan 18, 2010

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.