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

Unified Diff: src/harmony-string.js

Issue 401783003: Optimize algorithm for String.prototype.repeat(), fix value caching in others (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Clarify with comments Created 6 years, 5 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') | src/string.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..69223447d70f065b5c344ddd9c0bcaa676f4ccdc 100644
--- a/src/harmony-string.js
+++ b/src/harmony-string.js
@@ -21,12 +21,21 @@ function StringRepeat(count) {
throw MakeRangeError("invalid_count_value", []);
}
- var elements = new InternalArray(n);
- for (var i = 0; i < n; i++) {
- elements[i] = s;
+ // O(log n) algorithm
+
+ if (n < 1) return '';
aandrey 2014/07/30 09:04:01 nit: use ""
+
+ var res = '';
+
+ while (n > 1) {
aandrey 2014/07/30 09:04:01 +1 for a simpler version, since string concatenati
+ if (n & 1) res += s;
+ n >>= 1;
+ s += s;
}
- return %StringBuilderConcat(elements, n, "");
+ // unroll last iteration, no need to double the initial string again at the
+ // final iteration.
+ return res + s;
}
@@ -50,8 +59,7 @@ function StringStartsWith(searchString /* position */) { // length == 1
var s_len = s.length;
var start = MathMin(MathMax(pos, 0), s_len);
- var ss_len = ss.length;
- if (ss_len + start > s_len) {
+ if (ss.length + start > s_len) {
aandrey 2014/07/30 09:04:01 this should be a separate patch, to ease possible
return false;
}
@@ -81,8 +89,7 @@ function StringEndsWith(searchString /* position */) { // length == 1
}
var end = MathMin(MathMax(pos, 0), s_len);
- var ss_len = ss.length;
- var start = end - ss_len;
+ var start = end - ss.length;
if (start < 0) {
return false;
}
@@ -111,8 +118,7 @@ function StringContains(searchString /* position */) { // length == 1
var s_len = s.length;
var start = MathMin(MathMax(pos, 0), s_len);
- var ss_len = ss.length;
- if (ss_len + start > s_len) {
+ if (ss.length + start > s_len) {
return false;
}
« no previous file with comments | « AUTHORS ('k') | src/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698