Skip to content

Commit

Permalink
util: improve toUSVString performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed Aug 27, 2021
1 parent 27be963 commit 4d54445
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const {
Promise,
ReflectApply,
ReflectConstruct,
RegExpPrototypeExec,
RegExpPrototypeTest,
SafeMap,
SafeSet,
StringPrototypeSearch,
StringPrototypeReplace,
StringPrototypeToLowerCase,
StringPrototypeToUpperCase,
Expand Down Expand Up @@ -60,12 +60,10 @@ const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
const unpairedSurrogateRe = /\p{Surrogate}/u;
function toUSVString(val) {
const str = `${val}`;
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
// slower than `unpairedSurrogateRe.exec()`.
const match = RegExpPrototypeExec(unpairedSurrogateRe, str);
if (!match)
const index = StringPrototypeSearch(str, unpairedSurrogateRe);
if (index === -1)
return str;
return _toUSVString(str, match.index);
return _toUSVString(str, index);
}

let uvBinding;
Expand Down

0 comments on commit 4d54445

Please sign in to comment.