According to this MDN, it seems += is preferred over concat() for performance issues. So I use the following sample code to test.
var str = "";
var startTime = performance.now()
for(var i=0; i<10000000; i++) {
str = str.concat(i);
}
var endTime = performance.now()
console.log(`concat took ${endTime - startTime} ms`)
str = "";
startTime = performance.now()
for(var i=0; i<10000000; i++) {
str += i;
}
endTime = performance.now()
console.log(`+= took ${endTime - startTime} ms`)
But I don't see a significant difference until I increase the loop to 10,000,000 times. Here are some outputs I obtains when running on Chrome.
concat took 3067.600000023842 ms
+= took 2799 ms
concat took 2751.5 ms
+= took 2280.199999988079 ms
concat took 2865.699999988079 ms
+= took 1857.5999999642372 ms
concat took 2909.7000000476837 ms
+= took 2183.599999964237 ms
If you compare += and StringBuilder.append() with Java, you will observe a huge difference (e.g., 3 ms vs. 3000 ms) even testing on a loop of 10000 times.
So in which case can I observe a significant difference between += and concat() on JavaScript?