Array Join vs String Concat
String concatenation is faster in ECMAScript. Here's a benchmark I created to show you:
http://jsben.ch/#/OJ3vo
I can definitely say that using Array.join()
is faster. I've worked on a few pieces of JavaScript code and sped up performance significantly by removing string manipulation in favor of arrays.
From 2011 and into the modern day ...
See the following join
rewrite using string concatenation, and how much slower it is than the standard implementation.
// Number of times the standard `join` is faster, by Node.js versions:
// 0.10.44: ~2.0
// 0.11.16: ~4.6
// 0.12.13: ~4.7
// 4.4.4: ~4.66
// 5.11.0: ~4.75
// 6.1.0: Negative ~1.2 (something is wrong with 6.x at the moment)
function join(sep) {
var res = '';
if (this.length) {
res += this[0];
for (var i = 1; i < this.length; i++) {
res += sep + this[i];
}
}
return res;
}
The moral is - do not concatenate strings manually, always use the standard join
.