Challenge 06 — Unbounded in-memory cache

Find why a cache grows without ever evicting entries.

Mission

Every unique key is cached forever with no eviction.

DevTools checklist

  1. Take snapshot (A)
  2. Click Start leaking — wait ~10 seconds
  3. Force GC → snapshot (B) → Comparison
  4. Look for Map and Object growth

Expected signals

  • Map entry count grows
  • Object and Array deltas increase steadily
  • Retainer chain: global/module → Map → cached values

Hints & solution

Hint

A Map stores every fetched value under a new key. Nothing is ever deleted or expired.

Solution

Each tick adds a new key with a large array value to an unbounded Map. Without LRU eviction or TTL, the cache grows forever.

// Fix: bound the cache with LRU or TTL
const MAX = 100;
if (cache.size >= MAX) {
  const firstKey = cache.keys().next().value;
  cache.delete(firstKey);
}
cache.set(key, value);

← Lesson: Unbounded caches · All challenges