Cache Invalidation and Size Limits
You'll learn how to prevent memoization caches from consuming unbounded memory by implementing LRU (Least Recently Used) eviction and size limits.
The Problem with Unbounded Caches
Memoization improves performance by storing results, but every cached entry consumes memory. If your function is called with many unique arguments over time, the cache grows indefinitely, eventually causing memory issues or even crashes.
LRU Cache Strategy
An LRU (Least Recently Used) cache has a maximum size. When full, it evicts the least recently accessed entry before adding a new one. Think of it like a parking lot: when it's full, you remove the car that's been sitting there longest to make room for a newcomer.
Implementing Size Limits
Here's a memoize function with LRU caching:
function memoizeWithLimit(fn, maxSize = 100) {
const cache = new Map();
return function(...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
// Move to end (most recent)
const value = cache.get(key);
cache.delete(key);
cache.set(key, value);
return value;
}
const result = fn(...args);
cache.set(key, result);
// Evict oldest if over limit
if (cache.size > maxSize) {
const firstKey = cache.keys().next().value;
cache.delete(firstKey);
}
return result;
};
}
Map iteration order reflects insertion order. By deleting and re-inserting on access, we move entries to the "end." The first key becomes the oldest (least recently used).
When to Invalidate
You might also need manual invalidation when underlying data changes, or time-based expiration for stale results. These strategies prevent serving outdated cached values.
Key Takeaway: Always implement size limits and eviction policies in production memoization to prevent unbounded memory growth; LRU caching balances performance gains with controlled memory usage.