Reviewers: adamk,

Message:
This patch is basically a rebase of https://codereview.chromium.org/832713009 .

Description:
Optimize String.prototype.includes

This patch removes the MathMax call from String.prototype.includes
in order to improve performance. With some quick and dirty benchmarking,
(test case courtesy of the node folks) a sizable performance gain is visible:

d8> function testIndexOf() { var stringArray = [ 'hello', 'world', '123', 'abc' ]; return stringArray.some(function(val, idx, arr) { return val.indexOf('world')
!== -1 })}
d8> function testIncludes() { var stringArray = [ 'hello', 'world', '123', 'abc'
]; return stringArray.some(function(val, idx, arr) { return
val.includes('world') })}
d8> function testTime(fn) { var before = Date.now(); fn(); return Date.now() -
before; }
d8> testTime(function() { for (var i = 0; i < 10000000; i++) { testIncludes() }
})
2244
d8> testTime(function() { for (var i = 0; i < 10000000; i++) { testIndexOf() }
})
2212

Compare that to before the test, when the performance difference was much
larger:

d8> testTime(function() { for (var i = 0; i < 10000000; i++) { testIndexOf() }
})
2223
d8> testTime(function() { for (var i = 0; i < 10000000; i++) { testIncludes() }
})
2650

In my runs, performance of both functions drifts up and down, but running them
in quick
succession back and forth shows a roughly consistent delta of about this
magnitude.

String.prototype.includes is still slightly (maybe 5%) slower than
String.prototype.indexOf,
but the effect is significantly reduced.

R=adamk
BUG=v8:3807
LOG=Y

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

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

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


Index: src/string.js
diff --git a/src/string.js b/src/string.js
index 3ddd6d26cedfd9e49a5e624e071f0caeb005fba7..bf1af700b8cd711a354ecc8e9085b06812cc4f3c 100644
--- a/src/string.js
+++ b/src/string.js
@@ -1031,27 +1031,29 @@ function StringEndsWith(searchString /* position */) { // length == 1
 function StringIncludes(searchString /* position */) {  // length == 1
   CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes");

-  var s = TO_STRING_INLINE(this);
+  var string = TO_STRING_INLINE(this);

   if (IS_REGEXP(searchString)) {
throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.includes");
   }

-  var ss = TO_STRING_INLINE(searchString);
+  var searchString = TO_STRING_INLINE(searchString);
   var pos = 0;
   if (%_ArgumentsLength() > 1) {
     pos = %_Arguments(1);  // position
     pos = $toInteger(pos);
   }

-  var s_len = s.length;
-  var start = MathMin(MathMax(pos, 0), s_len);
-  var ss_len = ss.length;
-  if (ss_len + start > s_len) {
+  var stringLength = string.length;
+  if (pos < 0) pos = 0;
+  if (pos > stringLength) pos = stringLength;
+  var searchStringLength = searchStringLength.length;
+
+  if (searchStringLength + pos > stringLength) {
     return false;
   }

-  return %StringIndexOf(s, ss, start) !== -1;
+  return %StringIndexOf(string, searchString, pos) !== -1;
 }




--
--
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