Javascript string concatenation performance on modern browsers: array.join("") is now slower!

Well… as many Javascript developers know, a common practice to handle big string concatenation, is to implement a sort of  “stringBuffer” by using an array as data container and then convert it to a string by using the method join(). Ie:

var buffer = [];
var target = document.getElementById("box");

for (var i=0; i<10000; i++) {
    buffer .push(['<p id="node_', i, '" id="node_', i, '">Content ', i, '</p>'].join(""));

target.innerHTML = buffer.join("");

This is theoretically the fastest way to manage huge strings and to speed up the building of dynamic content.

It’s been a long time since I used this technique, but today I was doing some tests and I’ve realized that is not obsolete, but somehow inadequate to some modern browsers (Firefox 3, Opera 9 and Google Chrome), due to the worst performance compared to the old and classic string concatenation technique with the plus (+) operator. In my test I’ve compared the time required to concatenate a string built in a 10.000 steps loop with the time required to filling an array, and the amazing result is that, except for internet explorer (for the which buffer technique improve a lot the browser performance) and safari 3, other browser run faster with normal string concatenation with +… even 70/80% faster!
The results are the following:

– String concatenation with +:

  • 17000 milliseconds on Internet Explorer 6
  • 17000 milliseconds on Internet Explorer 7
  • 265 milliseconds on Opera 9
  • 125 milliseconds on Firefox 3
  • 165 milliseconds on Safari 3
  • 50 milliseconds on Google Chrome

– Array based technique:

  • 530 milliseconds on Internet Explorer 6 -> -16470 milliseconds (damned good!)
  • 470 milliseconds on Internet Explorer 7 -> -16530 milliseconds (damned good!)
  • 450 milliseconds on Opera 9 -> +185  milliseconds (very bad!)
  • 200 milliseconds on Firefox 3 -> +75 milliseconds (bad!)
  • 145 milliseconds on Safari 3 -> -20 milliseconds (good)
  • 95 milliseconds on Google Chrome -> +45 milliseconds (bad)

In conclusion:  if we want to get the best performances from Firefox, Opera and Chrome we have to avoid the array based technique (and use “+” operator instead),  however this technique is still valid if we don’t want to import a browser specific javascript file or branch the code from one browser to another, because it enhance meaningfully the code execution speed of Internet Explorer and decrease in an acceptable manner the execution on other browsers.

  • A lot of times, tests supposedly proving that push and join is better than += are misleading. Push and join may be better if you append one character a million times. In your test, you’re appending several characters each time, which in my opinion is a more reasonable test.

  • I believe the test will be much accurate if we do not wrap the “concat string” into another string buffer. Try this:

    for (var i=0; i<10000; i++) {
    buffer .push('Content ‘, i, ”);