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
ObeCheckObjectCoercible(this value).- Let
SbeToString(O).ReturnIfAbrupt(S).- Let
nbe the result of callingToInteger(count).ReturnIfAbrupt(n).- If
n < 0, then throw a RangeError exception.- If
nis+Infinity, then throw a RangeError exception.- Let
Tbe aStringvalue that is made fromncopies ofSappended together. Ifnis0,Tis 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.