typescript.unbounded_cache
Stability
High
Detects caches without size limits or eviction policies.
Why It Matters
Section titled “Why It Matters”Unbounded caches:
- Memory exhaustion — Cache grows until OOM
- No eviction — Stale data never removed
- Unpredictable behavior — Sudden crashes under load
Example
Section titled “Example”// ❌ 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;}What Unfault Detects
Section titled “What Unfault Detects”- Map/Object used as cache without size limits
- Missing LRU/TTL eviction
- Caches without capacity configuration
Auto-Fix
Section titled “Auto-Fix”Unfault can suggest replacing with bounded LRU cache.