Commit ce442660184 for nodejs

commit ce44266018462ef049446d9a343f0f83c7b8ec96
Author: Matteo Collina <hello@matteocollina.com>
Date:   Sat Nov 21 07:38:30 2026 +0100

    esm: avoid dictionary-mode objects in import()

    Import attributes were created in C++ with v8::Object::New() and a
    null prototype, once per module request and once per import() call.
    Every import() call also created a `{ __proto__: null }`
    diagnostics_channel context, which was discarded when module.import
    had no subscribers. V8 creates all of these in dictionary mode.

    Build the attributes from an ordinary object with its prototype set to
    null, and only create the trace context when there are subscribers.

    Signed-off-by: Matteo Collina <hello@matteocollina.com>
    PR-URL: https://github.com/nodejs/node/pull/67554
    Refs: https://github.com/nodejs/node/pull/67552
    Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
    Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
    Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
    Reviewed-By: James M Snell <jasnell@gmail.com>

diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js
index 80d6f875944..ff99764566a 100644
--- a/lib/internal/modules/esm/loader.js
+++ b/lib/internal/modules/esm/loader.js
@@ -520,6 +520,12 @@ class ModuleLoader {
    * @returns {Promise<ModuleExports>}
    */
   async import(specifier, parentURL, importAttributes, phase = kEvaluationPhase, isEntryPoint = false) {
+    // tracePromise() ignores the context when there are no subscribers, so only
+    // create it when there are: a `{ __proto__: null }` literal is created in
+    // dictionary mode, and import() can be called on a hot path.
+    const traceContext = onImport.hasSubscribers ?
+      { __proto__: null, parentURL, url: specifier } :
+      undefined;
     return onImport.tracePromise(async () => {
       const request = new ImportRequest(specifier, phase, importAttributes);
       const moduleJob = this.getOrCreateModuleJob(parentURL, request);
@@ -532,11 +538,7 @@ class ModuleLoader {
       }
       const { module } = await moduleJob.run(isEntryPoint);
       return module.getNamespace();
-    }, {
-      __proto__: null,
-      parentURL,
-      url: specifier,
-    });
+    }, traceContext);
   }

   /**
diff --git a/src/module_wrap.cc b/src/module_wrap.cc
index c680a85e8e0..4b881209c39 100644
--- a/src/module_wrap.cc
+++ b/src/module_wrap.cc
@@ -571,9 +571,13 @@ static Local<Object> createImportAttributesContainer(
     Local<FixedArray> raw_attributes,
     const int elements_per_attribute) {
   CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
-  size_t num_attributes = raw_attributes->Length() / elements_per_attribute;
-  LocalVector<Name> names(isolate, num_attributes);
-  LocalVector<Value> values(isolate, num_attributes);
+  Local<Context> context = realm->context();
+
+  // v8::Object::New() with a prototype argument always creates the object in
+  // dictionary mode. Null the prototype of an ordinary object instead, so that
+  // the attributes, which the loader reads for every import, get a fast map.
+  Local<Object> attributes = Object::New(isolate);
+  attributes->SetPrototype(context, Null(isolate)).Check();

   for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
     Local<Data> key = raw_attributes->Get(i);
@@ -581,15 +585,12 @@ static Local<Object> createImportAttributesContainer(
     DCHECK(DataIsString(key));
     DCHECK(DataIsString(value));

-    int idx = i / elements_per_attribute;
-    names[idx] = key.As<String>();
-    values[idx] = value.As<String>();
+    attributes
+        ->CreateDataProperty(context, key.As<String>(), value.As<String>())
+        .Check();
   }

-  Local<Object> attributes = Object::New(
-      isolate, Null(isolate), names.data(), values.data(), num_attributes);
-  attributes->SetIntegrityLevel(realm->context(), v8::IntegrityLevel::kFrozen)
-      .Check();
+  attributes->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen).Check();
   return attributes;
 }