What advantage does using WeakMap provide in this memoization implementation?
const memo = new WeakMap();
function memoize(fn) {
return function(obj) {
if (!memo.has(obj)) {
memo.set(obj, fn(obj));
}
return memo.get(obj);
};
}
WeakMap provides automatic cache cleanup in memoization: 1) Cached results are automatically removed when their corresponding input objects are garbage collected, 2) This prevents memory leaks that would occur with a regular Map-based cache, 3) No manual cache invalidation is needed, 4) The cache size naturally adjusts to only keep entries for live objects, 5) This is particularly valuable for long-running applications with changing object sets, 6) The implementation is memory-efficient as it doesn't prevent garbage collection, 7) It's ideal for pure functions that compute based on object properties, 8) The pattern demonstrates practical application of WeakMap's memory management features.