Skip to content

Commit af0d2cc

Browse files
committed
perf: read the cached private keys with one GetPrivate
HasPrivate followed by GetPrivate walked the object's private symbols twice per lookup; an absent private already reads back as undefined.
1 parent 3ac0522 commit af0d2cc

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

‎test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,21 @@ bool tns::V8SetPrivateValue(Isolate* isolate, const Local<Object>& obj, const Lo
113113
return res.FromMaybe(false);
114114
}
115115

116+
// An absent private reads as undefined, so one GetPrivate replaces the
117+
// HasPrivate + GetPrivate pair; callers never store undefined under these keys.
116118
bool tns::V8GetPrivateValue(Isolate* isolate, const Local<Object>& obj, const Local<Private>& privateKey, Local<Value>& out) {
117119
auto context = GetCreationContextOrCurrent(isolate, obj);
118-
auto hasPrivate = obj->HasPrivate(context, privateKey);
119-
120-
if (hasPrivate.IsNothing()) {
120+
Local<Value> value;
121+
if (!obj->GetPrivate(context, privateKey).ToLocal(&value)) {
121122
throw tns::NativeScriptException("Failed to Get Private Value");
122123
}
123124

124-
if (!hasPrivate.FromMaybe(false)) {
125+
if (value->IsUndefined()) {
125126
return false;
126127
}
127128

128-
auto res = obj->GetPrivate(context, privateKey);
129-
130-
if (res.IsEmpty()) {
131-
throw tns::NativeScriptException("Failed to Get Private Value");
132-
}
133-
134-
return res.ToLocal(&out);
129+
out = value;
130+
return true;
135131
}
136132

137133
bool tns::V8SetPrivateValue(Isolate* isolate, const Local<Object>& obj, const Local<Private>& privateKey, const Local<Value>& value) {

0 commit comments

Comments
 (0)