+ String运算符的性能低于StringBuffer.append()

问题描述:

在我的团队中,我们通常会像这样进行字符串连接:

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>";

但JS专家声称 + 运算符的性能低于 StringBuffer.append()。这是真的吗?

But the JS experts claim that the + operator is less performant than StringBuffer.append(). Is this really true?

Internet Explorer是当今世界上唯一真正受此影响的浏览器。 (版本5,6和7是狗慢.8没有显示相同的退化。)而且,你的字符串越长,IE变得越来越慢。

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.

如果你有连接的长字符串,那么肯定使用array.join技术。 (或者为了便于阅读,可以使用一些StringBuffer包装。)但如果你的字符串很短,请不要打扰。

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.