Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Unified Diff: src/harmony-string.js

Issue 657863002: Improve String.repeat. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « AUTHORS ('k') | test/mjsunit/harmony/string-repeat.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-string.js
diff --git a/src/harmony-string.js b/src/harmony-string.js
index ae13745cdbf55989ad78b288843396ee5fab36e9..e9b93d86a4f900503d3cae6fc8a8ed9575352ecd 100644
--- a/src/harmony-string.js
+++ b/src/harmony-string.js
@@ -17,16 +17,19 @@ function StringRepeat(count) {
var s = TO_STRING_INLINE(this);
var n = ToInteger(count);
- if (n < 0 || !NUMBER_IS_FINITE(n)) {
+ // The maximum string length is stored in a smi, so a longer repeat
+ // must result in a range error.
+ if (n < 0 || n > %_MaxSmi()) {
throw MakeRangeError("invalid_count_value", []);
}
- var elements = new InternalArray(n);
- for (var i = 0; i < n; i++) {
- elements[i] = s;
+ var r = "";
+ while (n > 0) {
+ if (n & 1) r += s;
+ n >>= 1;
+ s += s;
aandrey 2014/10/15 17:54:43 There will be extra unnecessary concatenation at t
}
-
- return %StringBuilderConcat(elements, n, "");
+ return r;
}
« no previous file with comments | « AUTHORS ('k') | test/mjsunit/harmony/string-repeat.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698