When implementing in-memory caching (e.g., memoization), use a Map for the cache and keep the cache implementation private.
Map instead of plain objects to avoid prototype/key pitfalls and to support non-string keys safely.returnedFn.cache = cache). External reads/writes can corrupt cached state and cause unpredictable results.Example (safe memoize):
const memoize = fn => {
const cache = new Map(); // private
return value => {
if (cache.has(value)) return cache.get(value);
const result = fn(value);
cache.set(value, result);
return result;
};
};