[英]JavaScript: String Concatenation slow performance? Array.join('')?
I've read that if I have a for
loop, I should not use string concation because it's slow. 我已经读过,如果我有一个
for
循环,我不应该使用字符串concation,因为它很慢。 Such as: 如:
for (i=0;i<10000000;i++) {
str += 'a';
}
And instead, I should use Array.join()
, since it's much faster: 相反,我应该使用
Array.join()
,因为它更快:
var tmp = [];
for (i=0;i<10000000;i++) {
tmp.push('a');
}
var str = tmp.join('');
However, I have also read that string concatention is ONLY a problem for Internet Explorer and that browsers such as Safari/Chrome, which use Webkit, actually perform FASTER is using string concatention than Array.join()
. 但是,我还读到字符串连接只是Internet Explorer的一个问题,而使用Webkit的Safari / Chrome等浏览器实际上执行速度更快的是使用字符串连接而不是
Array.join()
。
I've attempting to find a performance comparison between all major browser of string concatenation vs Array.join() and haven't been able to find one. 我试图找到字符串连接的所有主要浏览器与Array.join()之间的性能比较,但无法找到一个。
As such, what is faster and more efficient JavaScript code? 那么,什么是更快,更有效的JavaScript代码? Using string concatenation or Array.join()?
使用字符串连接或Array.join()?
It appears Array.join() for the most part across browsers is faster. 大多数浏览器中出现的Array.join()速度更快。
FF3 array.join is ~2x faster
Safari 3 array.join ~1.5x faster
Opera 9 += ~3x faster
ie6 array.join ~6x faster
ie7 array.join ~4x faster
FF3.1 += array.join equal in speed
Chrome += ~1.25x faster
IE8 array.join ~1.6x faster
Webkit array.join ~2x faster
Test results here: http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly 测试结果如下: http : //www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly
My feeling is that since there's no "nice" way to do a feature check to see whether string concatenation is slow, and because Array.join() is not terrible in non-IE browsers, using the array approach is good because your pages will behave pretty well everywhere and you won't have a mess to maintain (and you can avoid browser sniffing). 我的感觉是,由于没有“好”的方法来进行功能检查以查看字符串连接是否缓慢,并且因为Array.join()在非IE浏览器中并不可怕,所以使用数组方法很好,因为您的页面将会在任何地方都表现得很好,你不会乱七八糟地维护(你可以避免浏览器嗅探)。
Now if you're implementing a framework or a library and not just a feature or two on a site, then more fine tuning might be called for. 现在,如果您正在实现框架或库而不仅仅是站点上的一个或两个功能,那么可能需要进行更精细的调整。
Finally, I suggest going back to google and reading some of the results you get from a search for "javascript string concatenation performance." 最后,我建议回到google并阅读搜索“javascript string concatenation performance”所获得的一些结果。 I found several good ones and I only got through half the first SRP.
我发现了几个好的,我只通过了第一个SRP的一半。 Read the comments on the blog posts too because often you pick up interesting links there.
阅读博客文章中的评论,因为通常你会在那里找到有趣的链接。
I was curious not only about string concatenation, but about parsing as well. 我不仅对字符串连接感兴趣,而且对解析也感到好奇。 Here are some results regarding array.join, string+=, charAt() and array[].
以下是有关array.join,string + =,charAt()和array []的一些结果。
Short summary 简短的摘要
Raw results 原始结果
Chrome v23 FF 17 IE8
fill array(100k) 43 15 270 ms
join array(100k) 33 5 43 ms
string+=(100k) 223 12 118 ms
charAt(488.89k * 100) 517 462 75467 ms
split(488.89k) 12 34 279 ms
array[](488.89k * 100) 267 323 27343 ms
HTML code to test 要测试的HTML代码
<table>
<col/>
<col align="right"/>
<col/>
<script type="text/javascript" >
function test(what, action) {
var start = new Date(), end
document.write("<tr><td>" + what + "</td>")
action()
end = new Date()
document.write("<td>" + (end.getTime() - start.getTime()) + "</td> <td>ms</td></tr>\n")
}
var alen = 100000, efAlen, nIter = 100
var s = "" // test string for charAt, s[pos]
var ss = "" // subject ss += "??"
var as = [] // source for s = as.join("")
var slen // slen = s.length
var a // a = s.split("")
var maxSlen = 1000*1000
test("fill array(" + (alen / 1000) + "k)", function() {
slen = 0
for( var i = 0; i < alen; i++ ) {
var v = "" + i
slen += v.length
if( slen < maxSlen )
efAlen = (i + 1)
as[i] = v
}
})
test("join array(" + (efAlen / 1000) + "k)", function() {
s = as.slice(0, efAlen).join("")
slen = Math.min(s.length, maxSlen)
})
test("string+=(" + (efAlen / 1000) + "k)", function() {
for( var i = 0; i < efAlen; i++ ) {
ss += as[i]
}
if( ss != s )
document.write("ss.length:" + ss.length + ", s.length:" + s.length)
})
test("charAt(" + (slen / 1000) + "k * " + nIter + ")", function() {
for( var i = 0; i < nIter; i++ ) {
for( var p = 0; p < slen; p++ ) {
var c = s.charAt(p)
}
}
})
test("split(" + (slen / 1000) + "k)", function() {
a = s.split("")
})
test("array[](" + (slen / 1000) + "k * " + nIter + ")", function() {
for( var i = 0; i < nIter; i++ ) {
for( var p = 0; p < slen; p++ ) {
var c = a[p]
}
}
})
</script>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.