JavaScript: String Concatenation slow performance? Array.join('')?

Posted by NickNick on Stack Overflow See other posts from Stack Overflow or by NickNick
Published on 2010-05-06T14:10:35Z Indexed on 2010/05/06 14:48 UTC
Read the original article Hit count: 102

I've read that if I have a for loop, I should not use string concation because it's slow. Such as:

for (i=0;i<10000000;i++) {
    str += 'a';
}

And instead, I should use Array.join(), since it's much faster:

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().

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.

As such, what is faster and more efficient JavaScript code? Using string concatenation or Array.join()?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about Performance