Skip to content

Commit dd09c80

Browse files
committed
src: cache urlpattern properties
1 parent c7eb31d commit dd09c80

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/node_url_pattern.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,20 @@ URLPattern::URLPatternOptions::FromJsObject(Environment* env,
466466
return options;
467467
}
468468

469-
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
470-
MaybeLocal<Value> URLPattern::uppercase_name() const { \
471-
auto context = env()->context(); \
472-
return ToV8Value(context, url_pattern_.get_##lowercase_name()); \
469+
// Perform value lookup and cache the result in a v8::Global.
470+
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
471+
MaybeLocal<Value> URLPattern::uppercase_name() { \
472+
auto isolate = env()->isolate(); \
473+
if (lowercase_name.IsEmpty()) { \
474+
Local<Value> value; \
475+
if (ToV8Value(env()->context(), url_pattern_.get_##lowercase_name()) \
476+
.ToLocal(&value)) { \
477+
lowercase_name.Reset(isolate, value); \
478+
return value; \
479+
} \
480+
return {}; \
481+
} \
482+
return lowercase_name.Get(isolate); \
473483
}
474484
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
475485
#undef URL_PATTERN_COMPONENT_GETTERS
@@ -654,7 +664,7 @@ static void Initialize(Local<Object> target,
654664
SetProtoMethodNoSideEffect(isolate, ctor_tmpl, "exec", URLPattern::Exec);
655665
SetProtoMethodNoSideEffect(isolate, ctor_tmpl, "test", URLPattern::Test);
656666
SetConstructorFunction(context, target, "URLPattern", ctor_tmpl);
657-
}
667+
}
658668

659669
} // namespace node::url_pattern
660670

src/node_url_pattern.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ class URLPattern : public BaseObject {
9393
private:
9494
ada::url_pattern<URLPatternRegexProvider> url_pattern_;
9595
// Getter methods
96-
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
97-
v8::MaybeLocal<v8::Value> name() const;
96+
#define URL_PATTERN_COMPONENT_GETTERS(name, _) v8::MaybeLocal<v8::Value> name();
9897
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
9998
#undef URL_PATTERN_COMPONENT_GETTERS
10099
bool HasRegExpGroups() const;
@@ -107,6 +106,10 @@ class URLPattern : public BaseObject {
107106
Environment* env,
108107
const ada::url_pattern_input& input,
109108
std::optional<std::string_view>& baseURL); // NOLINT (runtime/references)
109+
110+
#define URL_PATTERN_CACHED_VALUES(_, name) v8::Global<v8::Value> name;
111+
URL_PATTERN_COMPONENTS(URL_PATTERN_CACHED_VALUES)
112+
#undef URL_PATTERN_CACHED_VALUES
110113
};
111114

112115
} // namespace node::url_pattern

0 commit comments

Comments
 (0)