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

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: 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
« AUTHORS ('K') | « 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 4cd8e6687edaeacb51079801b74480a21cc352b0..debdc7e6600aaf968e444aeb728efd34d1e780b9 100644
--- a/src/harmony-string.js
+++ b/src/harmony-string.js
@@ -21,12 +21,17 @@ function StringRepeat(count) {
throw MakeRangeError("invalid_count_value", []);
}
- var elements = new InternalArray(n);
- for (var i = 0; i < n; i++) {
- elements[i] = s;
- }
+ if (n < 1) return '';
Michael Starzinger 2014/07/28 11:13:52 This special case is not needed if comment below i
+
+ // Optimized O(log N) algorithm, without the added overhead of an extra array
Michael Starzinger 2014/07/28 11:13:51 nit: Let's drop (at least) the second half of the
+ var res = '';
- return %StringBuilderConcat(elements, n, "");
+ while (n > 1) {
Michael Starzinger 2014/07/28 11:13:51 If we change the predicate to be "n > 0" most spec
+ if (n & 1) res += s;
+ n >>= 1;
+ s += s;
+ }
+ return res + s;
Michael Starzinger 2014/07/28 11:13:51 This addition is not needed if comment above is ad
}
@@ -50,8 +55,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) {
return false;
}
@@ -81,8 +85,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 +114,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;
}
« AUTHORS ('K') | « AUTHORS ('k') | src/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698