Today, I read this thread about the speed of string concatenation.
Surprisingly, string concatenation was the winner:
http://jsben.ch/#/OJ3vo
The result was opposite of what I thought. Besides, there are many articles about this which explain oppositely like this.
I can guess that browsers are optimized to string concat
on the latest version, but how do they do that? Can we say that it is better to use +
when concatenating strings?
Update
So, in modern browsers string concatenation is optimized so using +
signs is faster than using join
when you want to concatenate strings.
But @Arthur pointed out that join
is faster if you actually want to join strings with a separator.
Update - 2020
Chrome: Array join
is almost 2 times faster
is String concat +
See: https://stackoverflow.com/a/54970240/984471
As a note:
- Array
join
is better if you have large strings
- If we need generate
several small strings
in final output, it is better to go with string concat +
, as otherwise going with Array will need several Array to String conversions at the end which is performance overload.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…