Skip to content
Prev Previous commit
Next Next commit
DRY
  • Loading branch information
aduh95 committed Jul 24, 2023
commit 56a8a07c7088bcf011a910c9adb1e18cf6e15794
26 changes: 11 additions & 15 deletions lib/internal/modules/esm/module_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,21 @@ class ResolveCache extends SafeMap {
',');
Comment thread
aduh95 marked this conversation as resolved.
}

#getInternalCache(parentURL) {
Comment thread
aduh95 marked this conversation as resolved.
Outdated
let internalCache = super.get(parentURL);
if (internalCache == null) {
super.set(parentURL, internalCache = { __proto__: null });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
super.set(parentURL, internalCache = { __proto__: null });
super.set(parentURL, internalCache = new SafeMap());

And associated changes.

Copy link
Copy Markdown
Contributor Author

@aduh95 aduh95 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to do that in this PR, if Map is faster than an object, we should update LoadCache as well and that's out of scope for this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. @JakobJingleheimer if you still do a refactor after this, maybe add this to the list of improvements?

}
return internalCache;
}

/**
* @param {string} serializedKey
* @param {string} parentURL
* @returns {import('./loader').ModuleExports | Promise<import('./loader').ModuleExports>}
*/
get(serializedKey, parentURL) {
let internalCache = super.get(parentURL);
if (internalCache == null) {
super.set(parentURL, internalCache = { __proto__: null });
}
return internalCache[serializedKey];
return this.#getInternalCache(parentURL)[serializedKey];
}

/**
Expand All @@ -63,20 +67,12 @@ class ResolveCache extends SafeMap {
* @param {{ format: string, url: URL['href'] }} result
*/
set(serializedKey, parentURL, result) {
let internalCache = super.get(parentURL);
if (internalCache == null) {
super.set(parentURL, internalCache = { __proto__: null });
}
internalCache[serializedKey] = result;
this.#getInternalCache(parentURL)[serializedKey] = result;
return this;
}

has(serializedKey, parentURL) {
let internalCache = super.get(parentURL);
if (internalCache == null) {
super.set(parentURL, internalCache = { __proto__: null });
}
return serializedKey in internalCache;
return serializedKey in this.#getInternalCache(parentURL);
}
}

Expand Down