I'm writing JavaScript unit tests and I need to create a string of length 65536. What's the best way to do this in JavaScript?
Currently I'm using:
var myString = '';
for (var i = 0; i <= 65535; ++i) {
myString += 'x';
}
I'm writing JavaScript unit tests and I need to create a string of length 65536. What's the best way to do this in JavaScript?
Currently I'm using:
var myString = '';
for (var i = 0; i <= 65535; ++i) {
myString += 'x';
}
This is a forward-looking answer, and won't work in current implementations.
ECMAScript 6 is currently defining a String.prototype.repeat
method. This will allow you to do:
var result = "x".repeat(65535);
Again, this is a future addition. Currently ECMAScript 6 (Harmony) is being drafted, and this could technically be removed, though it doesn't seem likely.
Current draft:
15.5.4.21 String.prototype.repeat (count)
The following steps are taken:
- Let
O
beCheckObjectCoercible(this value)
.- Let
S
beToString(O)
.ReturnIfAbrupt(S)
.- Let
n
be the result of callingToInteger(count)
.ReturnIfAbrupt(n)
.- If
n < 0
, then throw a RangeError exception.- If
n
is+Infinity
, then throw a RangeError exception.- Let
T
be aString
value that is made fromn
copies ofS
appended together. Ifn
is0
,T
is the empty String.- Return
T
.NOTE 1 This method creates a String consisting of the string elements of this object (converted to String) repeated count time.
NOTE 2 The repeat function is intentionally generic; it does not require that its this value be a String object.Therefore, it can be transferred to other kinds of objects for use as a method.
How about
Array(65537).join('x')
Note, that it's 65537, not 65536, because you put characters inbetween.
Array.prototype.join
doesn't have to be called on an Array, just an Object with a length property (tested in Google Chrome, FireFox, IE10)
function makeStr(len, char) {
return Array.prototype.join.call({length: (len || -1) + 1}, char || 'x');
}
makeStr(5); // "xxxxx"
This lets you benefit from native function making the string, without the overhead of a huge array.
Out of interest, I created a quick benchmark test on jsperf.com:
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);
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.
You could create an array of length whatver you want and then use the join()
method on the array which will make it into a string. Array(number).join(char)
this creates an array on size number -1
. Also note that you do not want to use your method because concatenating strings is very expensive(O(n) every concat). I am not sure if javascript has a StringBuilder like java