Create your own version of Java’s StringBuilder:
var StringBuilder = function (str) { var _stack = str ? [str] : []; // str == initial string to start, if any // append can take one or more strings as arguments this.append = function () { _stack.push.apply(_stack, arguments); return this; } this.toString = function (separator) { return _stack.join(separator || ''); } } |
This routine runs up to 8x faster in IE6 than normal string concatenation using the + and += operators! In IE7, I’ve clocked a 4x improvement.
Firefox 3.0 gets about a 20% speed boost. Safari 4 and Chrome get less than a 10% speed boost.
It would be an easy exercise to modify the StringBuilder class’ append method and constructor to take an existing StringBuilder object instead of a string.