Skip to content

Commit

Permalink
util: reduce javascript call for ToUSVString
Browse files Browse the repository at this point in the history
PR-URL: nodejs#47192
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
anonrig authored Mar 23, 2023
1 parent 8200304 commit f51c152
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
10 changes: 1 addition & 9 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,8 @@ const experimentalWarnings = new SafeSet();

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

const unpairedSurrogateRe =
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
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)
return str;
return _toUSVString(str, match.index);
return _toUSVString(`${val}`);
}

let uvBinding;
Expand Down
8 changes: 2 additions & 6 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,12 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {

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

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

int64_t start = args[1]->IntegerValue(env->context()).FromJust();
CHECK_GE(start, 0);

for (size_t i = start; i < value.length(); i++) {
for (size_t i = 0; i < value.length(); i++) {
char16_t c = value[i];
if (!IsUnicodeSurrogate(c)) {
continue;
Expand Down

0 comments on commit f51c152

Please sign in to comment.