From a7eedc232944bb15477595707086ee1b5b043cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerhard=20St=C3=B6bich?= Date: Mon, 3 Oct 2022 18:26:07 +0200 Subject: [PATCH] node-api: create reference only when needed Optimize napi_create_external() to create the reference for calling finalizer only if user actually provides a finalizer callback. PR-URL: https://github.com/nodejs/node/pull/44827 Reviewed-By: Luigi Pinca Reviewed-By: Daeyeon Jeong Reviewed-By: James M Snell --- src/js_native_api_v8.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index dacf01117a5069..58567c5e44a9e7 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2387,10 +2387,12 @@ napi_status NAPI_CDECL napi_create_external(napi_env env, v8::Local 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);