Reviewers: Dan Ehrenberg,

Description:
[es6] Use SubString in String{Starts,Ends}With

Much faster and constant than always searching the whole string

````
var allCodePoints = [];
for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
var allCharsString = String.fromCharCode.apply(String, allCodePoints);

function bench(search) {
  var counter = 0;
  print(search + " found at " + allCharsString.startsWith(search));
  var start = Date.now();
  while (counter++ < 5000000) {
    allCharsString.startsWith(search);
  }
  var end = Date.now();
  print(end - start);
  return counter;
}

print("single character");
bench("\u0000");
bench("\u0050");
bench("\u1000");
````

OLD

single character
 found at true
374
P found at false
559
က found at false
13492

NEW

single character
 found at true
261
P found at false
146
က found at false
146

BUG=

Please review this at https://codereview.chromium.org/1321853006/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+2, -2 lines):
  M src/string.js


Index: src/string.js
diff --git a/src/string.js b/src/string.js
index 1bbc8a69ce1259515bb950d2b259c5269056435a..1db65bb0bad25b677c7fa38144976c6a39f4ca18 100644
--- a/src/string.js
+++ b/src/string.js
@@ -997,7 +997,7 @@ function StringStartsWith(searchString /* position */) { // length == 1
     return false;
   }

-  return %StringIndexOf(s, ss, start) === start;
+  return %_SubString(s, start, start + ss_len) === ss;
 }


@@ -1028,7 +1028,7 @@ function StringEndsWith(searchString /* position */) { // length == 1
     return false;
   }

-  return %StringLastIndexOf(s, ss, start) === start;
+  return %_SubString(s, start, start + ss_len) === ss;
 }




--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to