CodePark/ questions / Creational patterns
START PRACTICINGPRACTICE
Backend question · Software design & patterns › Creational patterns

cache.ts contains export const cache = new Map(). Both a.ts and b.ts import it. How many Map instances exist at runtime?

One, as long as both imports resolve to the same file path

One. Node caches a module by its resolved filename. The top-level code runs once, and both files share the same Map. So "Two, because each importing file runs the module again" is wrong.

The caveat is how the path resolves. These cases can create separate instances:

• two copies of a package in
node_modules
• different spellings of the path on a case-insensitive file system