diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-02-13 22:40:50 +0300 |
---|---|---|
committer | Shelley Vohr <shelley.vohr@gmail.com> | 2020-02-27 19:45:37 +0300 |
commit | 57302f866e9ba954992ac32334b6dad3ebd4090f (patch) | |
tree | 1ee3a18e814d1928f1feeff43bacf3d219dfa26a /src/module_wrap.cc | |
parent | db291aaf065d5b96cf179f74be7bafb4d1023a6e (diff) |
src: prefer 3-argument Array::New()
This is nicer, because:
1. It reduces overall code size,
2. It’s faster, because `Object::Set()` calls are relatively slow, and
3. It helps avoid invalid `.Check()`/`.FromJust()` calls.
PR-URL: https://github.com/nodejs/node/pull/31775
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Diffstat (limited to 'src/module_wrap.cc')
-rw-r--r-- | src/module_wrap.cc | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 0bc32f7846b..68359178f4a 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -264,11 +264,11 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) { Local<Context> mod_context = obj->context_.Get(isolate); Local<Module> module = obj->module_.Get(isolate); - Local<Array> promises = Array::New(isolate, - module->GetModuleRequestsLength()); + const int module_requests_length = module->GetModuleRequestsLength(); + MaybeStackBuffer<Local<Value>, 16> promises(module_requests_length); // call the dependency resolve callbacks - for (int i = 0; i < module->GetModuleRequestsLength(); i++) { + for (int i = 0; i < module_requests_length; i++) { Local<String> specifier = module->GetModuleRequest(i); Utf8Value specifier_utf8(env->isolate(), specifier); std::string specifier_std(*specifier_utf8, specifier_utf8.length()); @@ -290,10 +290,11 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) { Local<Promise> resolve_promise = resolve_return_value.As<Promise>(); obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise); - promises->Set(mod_context, i, resolve_promise).Check(); + promises[i] = resolve_promise; } - args.GetReturnValue().Set(promises); + args.GetReturnValue().Set( + Array::New(isolate, promises.out(), promises.length())); } void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) { @@ -426,12 +427,13 @@ void ModuleWrap::GetStaticDependencySpecifiers( int count = module->GetModuleRequestsLength(); - Local<Array> specifiers = Array::New(env->isolate(), count); + MaybeStackBuffer<Local<Value>, 16> specifiers(count); for (int i = 0; i < count; i++) - specifiers->Set(env->context(), i, module->GetModuleRequest(i)).Check(); + specifiers[i] = module->GetModuleRequest(i); - args.GetReturnValue().Set(specifiers); + args.GetReturnValue().Set( + Array::New(env->isolate(), specifiers.out(), count)); } void ModuleWrap::GetError(const FunctionCallbackInfo<Value>& args) { |