diff --git a/src/builtin-entries.js b/src/builtin-entries.js index f0cc65f94..5e13842e5 100644 --- a/src/builtin-entries.js +++ b/src/builtin-entries.js @@ -126,7 +126,7 @@ const cache = shared.memoize.builtinEntries for (const id of builtinIds) { setDeferred(builtinEntries, id, () => { - const cached = cache[id] + const cached = cache.get(id) if (cached) { return cached @@ -135,7 +135,7 @@ for (const id of builtinIds) { const entry = createEntry(id) if (id !== "module") { - cache[id] = entry + cache.set(id, entry) } return entry diff --git a/src/builtin-modules.js b/src/builtin-modules.js index 5034f8e64..4935ee53e 100644 --- a/src/builtin-modules.js +++ b/src/builtin-modules.js @@ -34,7 +34,7 @@ function getExports(id) { for (const id of builtinIds) { setDeferred(builtinModules, id, () => { - const cached = cache[id] + const cached = cache.get(id) if (cached) { return cached @@ -46,7 +46,7 @@ for (const id of builtinIds) { mod.loaded = true if (id !== "module") { - cache[id] = mod + cache.set(id, mod) } return mod diff --git a/src/fs/realpath.js b/src/fs/realpath.js index 221789eb3..c8e048321 100644 --- a/src/fs/realpath.js +++ b/src/fs/realpath.js @@ -28,7 +28,8 @@ function init() { } const cache = shared.memoize.fsRealpath - const cached = cache[thePath] + + let cached = cache.get(thePath) if (cached) { return cached @@ -44,9 +45,12 @@ function init() { typeof binding.fs.realpath === "function" } - return cache[thePath] = useBinding + cached = useBinding ? realpathBinding(thePath) : realpathFallback(thePath) + + cache.set(thePath, cached) + return cached } function realpathBinding(thePath) { diff --git a/src/module/esm/resolve-filename.js b/src/module/esm/resolve-filename.js index 2610d4afa..177cb1e31 100644 --- a/src/module/esm/resolve-filename.js +++ b/src/module/esm/resolve-filename.js @@ -91,22 +91,21 @@ function resolveFilename(request, parent, isMain, options) { fromPath = "" } + const cache = shared.memoize.moduleESMResolveFilename + let cacheKey - if (isObject(options)) { - cacheKey = "" - } else { + if (! isObject(options)) { cacheKey = request + "\0" + fromPath + "\0" + (isMain ? "1" : "") - } - const cache = shared.memoize.moduleESMResolveFilename - const cached = cacheKey && cache[cacheKey] + const cached = cache.get(cacheKey) - if (cached) { - return cached + if (cached) { + return cached + } } if (isAbs) { @@ -172,7 +171,8 @@ function resolveFilename(request, parent, isMain, options) { if (! foundPath && Reflect.has(builtinLookup, decoded)) { - return cache[cacheKey] = decoded + cache.set(cacheKey, decoded) + return decoded } } @@ -183,7 +183,8 @@ function resolveFilename(request, parent, isMain, options) { isJS(foundPath) || isMJS(foundPath) || Reflect.has(strictExtsLookup, extname(foundPath))) { - return cache[cacheKey] = foundPath + cache.set(cacheKey, foundPath) + return foundPath } throw new ERR_UNKNOWN_FILE_EXTENSION(foundPath) diff --git a/src/module/internal/find-path.js b/src/module/internal/find-path.js index 8055cc84e..6d16cc3da 100644 --- a/src/module/internal/find-path.js +++ b/src/module/internal/find-path.js @@ -55,7 +55,7 @@ function findPath(request, paths, isMain, fields, exts) { } const cache = shared.memoize.moduleInternalFindPath - const cached = cache[cacheKey] + const cached = cache.get(cacheKey) if (cached !== void 0) { return cached @@ -172,7 +172,8 @@ function findPath(request, paths, isMain, fields, exts) { } if (filename) { - return cache[cacheKey] = filename + cache.set(cacheKey, filename) + return filename } } diff --git a/src/module/internal/read-package.js b/src/module/internal/read-package.js index 13d7845b0..38fd92a91 100644 --- a/src/module/internal/read-package.js +++ b/src/module/internal/read-package.js @@ -1,4 +1,5 @@ import GenericArray from "../../generic/array.js" +import GenericObject from "../../generic/object.js" import SafeJSON from "../../safe/json.js" import readFileFast from "../../fs/read-file-fast.js" @@ -18,8 +19,10 @@ function readPackage(dirPath, fields) { cacheKey += "\0" + (fieldsLength === 1 ? fields[0] : GenericArray.join(fields)) } - if (Reflect.has(cache, cacheKey)) { - return cache[cacheKey] + let cached = cache.get(cacheKey) + + if (cached) { + return cached } const jsonPath = dirPath + sep + "package.json" @@ -33,12 +36,17 @@ function readPackage(dirPath, fields) { } try { - return cache[cacheKey] = SafeJSON.parse(jsonString) + cached = + SafeJSON.parse(jsonString) || + GenericObject.create() } catch (e) { e.path = jsonPath e.message = "Error parsing " + jsonPath + ": " + toString(e.message) throw e } + + cache.set(cacheKey, cached) + return cached } export default readPackage diff --git a/src/module/static/resolve-filename.js b/src/module/static/resolve-filename.js index cd79f1754..5b4434b45 100644 --- a/src/module/static/resolve-filename.js +++ b/src/module/static/resolve-filename.js @@ -61,22 +61,21 @@ function resolveFilename(request, parent, isMain, options) { fromPath = "" } + const cache = shared.memoize.moduleStaticResolveFilename + let cacheKey - if (isObject(options)) { - cacheKey = "" - } else { + if (! isObject(options)) { cacheKey = request + "\0" + fromPath + "\0" + (isMain ? "1" : "") - } - const cache = shared.memoize.moduleStaticResolveFilename - const cached = cacheKey && cache[cacheKey] + const cached = cache.get(cacheKey) - if (cached) { - return cached + if (cached) { + return cached + } } if (isAbs) { @@ -102,9 +101,11 @@ function resolveFilename(request, parent, isMain, options) { const foundPath = Module._findPath(request, paths, isMain) if (foundPath) { - return cacheKey - ? cache[cacheKey] = foundPath - : foundPath + if (cacheKey) { + cache.set(cacheKey, foundPath) + } + + return foundPath } throw new MODULE_NOT_FOUND(request) diff --git a/src/shared.js b/src/shared.js index faf0bcabf..b6b411956 100644 --- a/src/shared.js +++ b/src/shared.js @@ -62,21 +62,21 @@ function init() { external: __external__, inited: false, memoize: { - builtinEntries: { __proto__: null }, - builtinModules: { __proto__: null }, - fsRealpath: { __proto__: null }, - moduleESMResolveFilename: { __proto__: null }, - moduleInternalFindPath: { __proto__: null }, - moduleInternalReadPackage: { __proto__: null }, - moduleStaticResolveFilename: { __proto__: null }, + builtinEntries: new Map, + builtinModules: new Map, + fsRealpath: new Map, + moduleESMResolveFilename: new Map, + moduleInternalFindPath: new Map, + moduleInternalReadPackage: new Map, + moduleStaticResolveFilename: new Map, shimFunctionPrototypeToString: new WeakMap, shimProcessBindingUtilGetProxyDetails: new WeakMap, utilGetProxyDetails: new WeakMap, utilMaskFunction: new WeakMap, - utilMaxSatisfying: { __proto__: null }, - utilParseURL: { __proto__: null }, + utilMaxSatisfying: new Map, + utilParseURL: new Map, utilProxyExports: new WeakMap, - utilSatisfies: { __proto__: null }, + utilSatisfies: new Map, utilUnwrapOwnProxy: new WeakMap, utilUnwrapProxy: new WeakMap }, diff --git a/src/util/max-satisfying.js b/src/util/max-satisfying.js index 8a65753e5..a2403015b 100644 --- a/src/util/max-satisfying.js +++ b/src/util/max-satisfying.js @@ -16,11 +16,15 @@ function init() { range const cache = shared.memoize.utilMaxSatisfying - const cached = cache[cacheKey] - return cached === void 0 - ? cache[cacheKey] = _maxSatisfying(versions, range) - : cached + let cached = cache.get(cacheKey) + + if (cached === void 0) { + cached = _maxSatisfying(versions, range) + cache.set(cacheKey, cached) + } + + return cached } return maxSatisfying diff --git a/src/util/parse-url.js b/src/util/parse-url.js index 240cb2756..dff8e650c 100644 --- a/src/util/parse-url.js +++ b/src/util/parse-url.js @@ -27,7 +27,14 @@ function init() { const cacheKey = typeof url === "string" ? url : "" const cache = shared.memoize.utilParseURL - return cache[cacheKey] || (cache[cacheKey] = parse(url)) + let cached = cache.get(cacheKey) + + if (cached === void 0) { + cached = parse(url) + cache.set(cacheKey, cached) + } + + return cached } return parseURL diff --git a/src/util/satisfies.js b/src/util/satisfies.js index 2326d4158..6d7589ff6 100644 --- a/src/util/satisfies.js +++ b/src/util/satisfies.js @@ -11,11 +11,15 @@ function init() { const cacheKey = version + "\0" + range const cache = shared.memoize.utilSatisfies - const cached = cache[cacheKey] - return cached === void 0 - ? cache[cacheKey] = _satisfies(stripPrereleaseTag(version), range) - : cached + let cached = cache.get(cacheKey) + + if (cached === void 0) { + cached = _satisfies(stripPrereleaseTag(version), range) + cache.set(cacheKey, cached) + } + + return cached } return satisfies