Skip to content

Commit

Permalink
node-api: create reference only when needed
Browse files Browse the repository at this point in the history
Optimize napi_create_external() to create the reference for calling
finalizer only if user actually provides a finalizer callback.

PR-URL: #44827
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Flarna authored and danielleadams committed Oct 5, 2022
1 parent dc2af26 commit a854bb3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2387,10 +2387,12 @@ napi_status NAPI_CDECL napi_create_external(napi_env env,

v8::Local<v8::Value> external_value = v8::External::New(isolate, data);

// The Reference object will delete itself after invoking the finalizer
// callback.
v8impl::Reference::New(
env, external_value, 0, true, finalize_cb, data, finalize_hint);
if (finalize_cb) {
// The Reference object will delete itself after invoking the finalizer
// callback.
v8impl::Reference::New(
env, external_value, 0, true, finalize_cb, data, finalize_hint);
}

*result = v8impl::JsValueFromV8LocalValue(external_value);

Expand Down

0 comments on commit a854bb3

Please sign in to comment.