Skip to content

typescript.unbounded_cache

Stability High

Detects caches without size limits or eviction policies.

Unbounded caches:

  • Memory exhaustion — Cache grows until OOM
  • No eviction — Stale data never removed
  • Unpredictable behavior — Sudden crashes under load
// ❌ Before (unbounded Map cache)
const cache = new Map<string, Data>();
function getOrFetch(key: string): Data {
if (cache.has(key)) {
return cache.get(key)!;
}
const data = fetchData(key);
cache.set(key, data); // Grows forever!
return data;
}
// ✅ After (bounded LRU cache)
import LRU from 'lru-cache';
const cache = new LRU<string, Data>({
max: 500,
ttl: 1000 * 60 * 5, // 5 minutes
});
function getOrFetch(key: string): Data {
const cached = cache.get(key);
if (cached) return cached;
const data = fetchData(key);
cache.set(key, data);
return data;
}
  • Map/Object used as cache without size limits
  • Missing LRU/TTL eviction
  • Caches without capacity configuration

Unfault can suggest replacing with bounded LRU cache.