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

Side by Side 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, 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « AUTHORS ('k') | src/string.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 // This file relies on the fact that the following declaration has been made 7 // This file relies on the fact that the following declaration has been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $String = global.String; 9 // var $String = global.String;
10 // var $Array = global.Array; 10 // var $Array = global.Array;
11 11
12 // ------------------------------------------------------------------- 12 // -------------------------------------------------------------------
13 13
14 // ES6 draft 01-20-14, section 21.1.3.13 14 // ES6 draft 01-20-14, section 21.1.3.13
15 function StringRepeat(count) { 15 function StringRepeat(count) {
16 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat"); 16 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
17 17
18 var s = TO_STRING_INLINE(this); 18 var s = TO_STRING_INLINE(this);
19 var n = ToInteger(count); 19 var n = ToInteger(count);
20 if (n < 0 || !NUMBER_IS_FINITE(n)) { 20 if (n < 0 || !NUMBER_IS_FINITE(n)) {
21 throw MakeRangeError("invalid_count_value", []); 21 throw MakeRangeError("invalid_count_value", []);
22 } 22 }
23 23
24 var elements = new InternalArray(n); 24 // O(log n) algorithm
25 for (var i = 0; i < n; i++) { 25
26 elements[i] = s; 26 if (n < 1) return '';
aandrey 2014/07/30 09:04:01 nit: use ""
27
28 var res = '';
29
30 while (n > 1) {
aandrey 2014/07/30 09:04:01 +1 for a simpler version, since string concatenati
31 if (n & 1) res += s;
32 n >>= 1;
33 s += s;
27 } 34 }
28 35
29 return %StringBuilderConcat(elements, n, ""); 36 // unroll last iteration, no need to double the initial string again at the
37 // final iteration.
38 return res + s;
30 } 39 }
31 40
32 41
33 // ES6 draft 04-05-14, section 21.1.3.18 42 // ES6 draft 04-05-14, section 21.1.3.18
34 function StringStartsWith(searchString /* position */) { // length == 1 43 function StringStartsWith(searchString /* position */) { // length == 1
35 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith"); 44 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
36 45
37 var s = TO_STRING_INLINE(this); 46 var s = TO_STRING_INLINE(this);
38 47
39 if (IS_REGEXP(searchString)) { 48 if (IS_REGEXP(searchString)) {
40 throw MakeTypeError("first_argument_not_regexp", 49 throw MakeTypeError("first_argument_not_regexp",
41 ["String.prototype.startsWith"]); 50 ["String.prototype.startsWith"]);
42 } 51 }
43 52
44 var ss = TO_STRING_INLINE(searchString); 53 var ss = TO_STRING_INLINE(searchString);
45 var pos = 0; 54 var pos = 0;
46 if (%_ArgumentsLength() > 1) { 55 if (%_ArgumentsLength() > 1) {
47 pos = %_Arguments(1); // position 56 pos = %_Arguments(1); // position
48 pos = ToInteger(pos); 57 pos = ToInteger(pos);
49 } 58 }
50 59
51 var s_len = s.length; 60 var s_len = s.length;
52 var start = MathMin(MathMax(pos, 0), s_len); 61 var start = MathMin(MathMax(pos, 0), s_len);
53 var ss_len = ss.length; 62 if (ss.length + start > s_len) {
aandrey 2014/07/30 09:04:01 this should be a separate patch, to ease possible
54 if (ss_len + start > s_len) {
55 return false; 63 return false;
56 } 64 }
57 65
58 return %StringIndexOf(s, ss, start) === start; 66 return %StringIndexOf(s, ss, start) === start;
59 } 67 }
60 68
61 69
62 // ES6 draft 04-05-14, section 21.1.3.7 70 // ES6 draft 04-05-14, section 21.1.3.7
63 function StringEndsWith(searchString /* position */) { // length == 1 71 function StringEndsWith(searchString /* position */) { // length == 1
64 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); 72 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
65 73
66 var s = TO_STRING_INLINE(this); 74 var s = TO_STRING_INLINE(this);
67 75
68 if (IS_REGEXP(searchString)) { 76 if (IS_REGEXP(searchString)) {
69 throw MakeTypeError("first_argument_not_regexp", 77 throw MakeTypeError("first_argument_not_regexp",
70 ["String.prototype.endsWith"]); 78 ["String.prototype.endsWith"]);
71 } 79 }
72 80
73 var ss = TO_STRING_INLINE(searchString); 81 var ss = TO_STRING_INLINE(searchString);
74 var s_len = s.length; 82 var s_len = s.length;
75 var pos = s_len; 83 var pos = s_len;
76 if (%_ArgumentsLength() > 1) { 84 if (%_ArgumentsLength() > 1) {
77 var arg = %_Arguments(1); // position 85 var arg = %_Arguments(1); // position
78 if (!IS_UNDEFINED(arg)) { 86 if (!IS_UNDEFINED(arg)) {
79 pos = ToInteger(arg); 87 pos = ToInteger(arg);
80 } 88 }
81 } 89 }
82 90
83 var end = MathMin(MathMax(pos, 0), s_len); 91 var end = MathMin(MathMax(pos, 0), s_len);
84 var ss_len = ss.length; 92 var start = end - ss.length;
85 var start = end - ss_len;
86 if (start < 0) { 93 if (start < 0) {
87 return false; 94 return false;
88 } 95 }
89 96
90 return %StringLastIndexOf(s, ss, start) === start; 97 return %StringLastIndexOf(s, ss, start) === start;
91 } 98 }
92 99
93 100
94 // ES6 draft 04-05-14, section 21.1.3.6 101 // ES6 draft 04-05-14, section 21.1.3.6
95 function StringContains(searchString /* position */) { // length == 1 102 function StringContains(searchString /* position */) { // length == 1
96 CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains"); 103 CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains");
97 104
98 var s = TO_STRING_INLINE(this); 105 var s = TO_STRING_INLINE(this);
99 106
100 if (IS_REGEXP(searchString)) { 107 if (IS_REGEXP(searchString)) {
101 throw MakeTypeError("first_argument_not_regexp", 108 throw MakeTypeError("first_argument_not_regexp",
102 ["String.prototype.contains"]); 109 ["String.prototype.contains"]);
103 } 110 }
104 111
105 var ss = TO_STRING_INLINE(searchString); 112 var ss = TO_STRING_INLINE(searchString);
106 var pos = 0; 113 var pos = 0;
107 if (%_ArgumentsLength() > 1) { 114 if (%_ArgumentsLength() > 1) {
108 pos = %_Arguments(1); // position 115 pos = %_Arguments(1); // position
109 pos = ToInteger(pos); 116 pos = ToInteger(pos);
110 } 117 }
111 118
112 var s_len = s.length; 119 var s_len = s.length;
113 var start = MathMin(MathMax(pos, 0), s_len); 120 var start = MathMin(MathMax(pos, 0), s_len);
114 var ss_len = ss.length; 121 if (ss.length + start > s_len) {
115 if (ss_len + start > s_len) {
116 return false; 122 return false;
117 } 123 }
118 124
119 return %StringIndexOf(s, ss, start) !== -1; 125 return %StringIndexOf(s, ss, start) !== -1;
120 } 126 }
121 127
122 128
123 // ES6 Draft 05-22-2014, section 21.1.3.3 129 // ES6 Draft 05-22-2014, section 21.1.3.3
124 function StringCodePointAt(pos) { 130 function StringCodePointAt(pos) {
125 CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt"); 131 CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt");
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 InstallFunctions($String.prototype, DONT_ENUM, $Array( 188 InstallFunctions($String.prototype, DONT_ENUM, $Array(
183 "codePointAt", StringCodePointAt, 189 "codePointAt", StringCodePointAt,
184 "contains", StringContains, 190 "contains", StringContains,
185 "endsWith", StringEndsWith, 191 "endsWith", StringEndsWith,
186 "repeat", StringRepeat, 192 "repeat", StringRepeat,
187 "startsWith", StringStartsWith 193 "startsWith", StringStartsWith
188 )); 194 ));
189 } 195 }
190 196
191 ExtendStringPrototype(); 197 ExtendStringPrototype();
OLDNEW
« 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