module: keep ESM load cache entries in fast mode

Keep per-URL load cache entries in fast-property mode while
   preserving their null prototype.

Assisted-by: Pi
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/66276
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
Matteo Collina authored and Antoine du Hamel committed 2026-11-25 18:05:55 +01:00
1 parent f5264fad43
commit 4ce7129eb4
3 files changed
+50 -1

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
urls: [1, 100],
n: [1e7],
}, {
flags: ['--expose-internals'],
});
function main({ urls, n }) {
const { LoadCache } = require('internal/modules/esm/module_map');
const cache = new LoadCache();
const job = () => {};
const keys = Array.from({ length: urls }, (_, i) => `file:///module-${i}.mjs`);
for (let i = 0; i < keys.length; i++) cache.set(keys[i], undefined, job);
bench.start();
for (let i = 0; i < n; i++) {
if (cache.get(keys[i % keys.length]) !== job) {
throw new Error('Load cache miss');
}
}
bench.end(n);
}
+2 -1
View File
@@ -6,6 +6,7 @@ const {
ArrayPrototypeSort,
JSONStringify,
ObjectKeys,
ObjectSetPrototypeOf,
SafeMap,
} = primordials;
const { kImplicitTypeAttribute } = require('internal/modules/esm/assert');
@@ -108,7 +109,7 @@ class LoadCache extends SafeMap {
debug(`Storing ${url} (${
type === kImplicitTypeAttribute ? 'implicit type' : type
}) in ModuleLoadMap`);
const cachedJobsForUrl = super.get(url) ?? { __proto__: null };
const cachedJobsForUrl = super.get(url) ?? ObjectSetPrototypeOf({}, null);
cachedJobsForUrl[type] = job;
return super.set(url, cachedJobsForUrl);
}
@@ -0,0 +1,22 @@
'use strict';
// Flags: --expose-internals
require('../common');
const assert = require('assert');
const { LoadCache } = require('internal/modules/esm/module_map');
const cache = new LoadCache();
const url = 'file:///module.mjs';
const job = () => {};
assert.strictEqual(cache.get(url, '__proto__'), undefined);
assert.strictEqual(cache.has(url, 'constructor'), false);
cache.set(url, '__proto__', job);
assert.strictEqual(cache.get(url, '__proto__'), job);
assert.strictEqual(cache.has(url, '__proto__'), true);
assert.strictEqual(cache.has(url, 'constructor'), false);
cache.set(url, 'constructor', job);
assert.strictEqual(cache.get(url, 'constructor'), job);
cache.delete(url, '__proto__');
assert.strictEqual(cache.get(url, '__proto__'), undefined);