Posted By: Don
On my team, we usually do string concatentation like this:
var url = // some dynamically generated URL var sb = new StringBuffer(); sb.append("<a href='").append(url).append("'>click here</a>");
Obviously the following is much more readable:
var url = // some dynamically generated URL var sb = "<a href='" + url + "'>click here</a>";
But the JS experts claim that the
+ operator is less performant than
StringBuffer.append(). Is this really true?
Internet Explorer is the only browser which really suffers from this in today’s world. (Versions 5, 6, and 7 were dog slow. 8 does not show the same degradation.) What’s more, IE gets slower and slower the longer your string is.
If you have long strings to concatenate then definitely use an array.join technique. (Or some StringBuffer wrapper around this, for readability.) But if your strings are short don’t bother.