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

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: Replace bitwise with arithmetic operators Created 6 years, 4 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') | no next file » | 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..5aa1975340c2bf6ae97f930191e27a7677167336 100644
--- a/src/harmony-string.js
+++ b/src/harmony-string.js
@@ -21,12 +21,26 @@ 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 "";
+
+ var res = "";
+
+ while (n > 1) {
+ if (n % 2) res += s;
Yang 2014/08/19 12:29:00 two-character indent please.
+ n /= 2;
+ s += s;
}
- return %StringBuilderConcat(elements, n, "");
+ // unroll last iteration, no need to double the initial string again at the
Yang 2014/08/19 12:29:00 Capitalize "unroll".
+ // final iteration.
+
+ // This flattens the string. It is a small overhead to significantly improve
+ // string handling
+ var array = new InternalArray(1);
+ array[0] = res + s;
+ return %StringBuilderConcat(array, 1, "");
Yang 2014/08/19 12:29:00 Use %FlattenString
}
@@ -81,8 +95,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 +124,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') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698