Out of interest, I created a quick benchmark test on jsperf.com:
http://jsperf.com/mkstring
Contestants are
Array(x).join()
Array.prototype.join posted by Paul S.
strRepeat from underscore.string
function strRepeat(str, qty) {
if (qty < 1) return '';
var result = '';
while (qty > 0) {
if (qty & 1) result += str;
qty >>= 1, str += str;
}
return result;
}
strRepeat('*', 20000);
- EMCAScript 6 String.repeat mentioned by Crayz Train
For Firefox 34 (which already supports the ECMAScript6 String.repeat), the native repeat is the fastest, followed by strRepeat.
Interestingly with Chrom(e|ium) 39 the strRepeat function is even faster compared to the native String.repeat function of Firefox in my test runs.
For all tested browsers, the function proposed by Paul S. using the native Array.join method is slower than the strRepeat function of the underscore.string library. So, don't use it if you're looking for a fast method.