Bug 897305 - Improve String.prototype.repeat() performance; r=tschneidereit

This commit is contained in:
Joseph Myers
2013-08-03 09:57:53 +05:30
parent c00021fbd7
commit 54771eabec
3 changed files with 32 additions and 16 deletions

View File

@@ -17,19 +17,27 @@ function String_repeat(count) {
var n = ToInteger(count);
// Steps 6-7.
if (n < 0 || n === std_Number_POSITIVE_INFINITY)
ThrowError(JSMSG_REPEAT_RANGE);
if (n < 0)
ThrowError(JSMSG_NEGATIVE_REPETITION_COUNT); // a RangeError
// Step 8-9.
if (n === 0)
return "";
if (!(n * S.length < (1 << 28)))
ThrowError(JSMSG_RESULTING_STRING_TOO_LARGE); // a RangeError
var result = S;
for (var i = 1; i <= n / 2; i *= 2)
result += result;
for (; i < n; i++)
result += S;
return result;
// Communicate |n|'s possible range to the compiler.
n = n & ((1 << 28) - 1);
// Steps 8-9.
var T = '';
for (;;) {
if (n & 1)
T += S;
n >>= 1;
if (n)
S += S;
else
break;
}
return T;
}
/**