Skip to content

Commit 4c9ec98

Browse files
authored
Revert "util: reduce javascript call for ToUSVString"
This reverts commit f51c152. PR-URL: #47340 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent aa5eb58 commit 4c9ec98

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/internal/util.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@ const experimentalWarnings = new SafeSet();
6969

7070
const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
7171

72+
const unpairedSurrogateRe =
73+
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
7274
function toUSVString(val) {
73-
return _toUSVString(`${val}`);
75+
const str = `${val}`;
76+
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
77+
// slower than `unpairedSurrogateRe.exec()`.
78+
const match = RegExpPrototypeExec(unpairedSurrogateRe, str);
79+
if (!match)
80+
return str;
81+
return _toUSVString(str, match.index);
7482
}
7583

7684
let uvBinding;

src/node_util.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,16 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
319319

320320
static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
321321
Environment* env = Environment::GetCurrent(args);
322-
CHECK_GE(args.Length(), 1);
322+
CHECK_GE(args.Length(), 2);
323323
CHECK(args[0]->IsString());
324+
CHECK(args[1]->IsNumber());
324325

325326
TwoByteValue value(env->isolate(), args[0]);
326327

327-
for (size_t i = 0; i < value.length(); i++) {
328+
int64_t start = args[1]->IntegerValue(env->context()).FromJust();
329+
CHECK_GE(start, 0);
330+
331+
for (size_t i = start; i < value.length(); i++) {
328332
char16_t c = value[i];
329333
if (!IsUnicodeSurrogate(c)) {
330334
continue;

0 commit comments

Comments
 (0)