+ String运算符的性能低于StringBuffer.append() [英] is the + operator less performant than StringBuffer.append()

查看:136
本文介绍了+ 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.

这篇关于+ String运算符的性能低于StringBuffer.append()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆