Using a plain Map (or {}) as an in-memory cache in the browser is quick and easy, but it comes with a handful of gotchas that you’ll eventually run into as your app grows.
1. It lives only as long as the page lives (✈️ “volatile”)
• A Map is RAM-only.
• Navigate away, refresh, crash the tab, or open a new tab and—poof—your cache is gone.
• If you need data to survive reloads or to be shared between tabs, you’ll have to add a second layer (e.g. localStorage, IndexedDB, service-worker, etc.).
2. No built-in eviction, TTL, or size control (🛑 “infinite growth”)
• A Map happily accepts keys forever.
• If you forget to prune, the tab’s memory usage keeps growing → performance hits or the browser kills the tab.
• You have to hand-roll LRU/LFU/TTL logic yourself or pull in a library.
3. Easy to create memory leaks (📈)
Example:
js
const cache = new Map();
function rememberDomNode(node) {
cache.set(node, expensiveCalculation(node));
}
Keeping a DOM node as a key makes that node non-garbage-collectable until you delete the entry. If you forget, those nodes—and maybe large sub-trees—stick around in memory.
(Using WeakMap can help here, but that only works if the key itself is what you want to cache against.)
4. Stale data & invalidation pain (⏳)
• Network data changes, auth tokens expire, feature flags flip, etc.
• A plain Map gives you no help deciding when an entry is “too old.”
• In a multi-tab scenario every tab has its own copy, so keeping them in sync is extra work (e.g. BroadcastChannel, storage events).
5. Not serializable / structured-clone-friendly (📦)
• You can’t do JSON.stringify(myMap) (it’ll become {}), and structured cloning a Map wipes the prototype information in some contexts.
• If you decide to persist it later, you’ll have to implement custom serialization/deserialization.
6. Key identity quirks (🔑)
• In a Map, object keys are compared by reference, not value.
js
const m = new Map();
m.set({ id: 1 }, 'foo');
m.get({ id: 1 }); // => undefined (different object)
It’s easy to accidentally miss cache hits if you construct new objects each time instead of re-using references or normalizing to strings.
7. Security / isolation concerns (🔒)
• If sensitive data (e.g. user tokens) sit in a Map, browser dev-tools or XSS attacks can read them during that session.
• IndexedDB offers some mitigation via same-origin rules and service-worker isolation; a plain in-memory map does not.
8. Debugging & observability (🔍)
• Hard to inspect large maps visually—console.log(cache) only goes so far.
• No built-in events for “entry added/removed” to hook into.
• Libraries such as react-query, swr, or redux give you dev-tools & instrumentation for free.
9. No concurrency guarantees (⚙️)
JavaScript is single-threaded, but in real apps you have WebWorkers, service workers, & async code. If multiple contexts mutate the same Map (e.g. you pass it via postMessage), you need to handle race conditions yourself.
TL;DR
A simple Map is fine for:
• Small, short-lived, non-critical data
• Single-tab utilities (memoizing a function, caching layout calculations, etc.)
But for anything that needs:
• Persistence across refreshes/tabs/users
• Size limits & eviction policies
• Observability and invalidation rules
…reach for a purpose-built solution (IndexedDB, localStorage + TTL, libraries like localForage, idb-keyval, react-query, etc.) or wrap your Map in a tiny LRU/TTL cache.