Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumJavaScriptclosures as a patternSingle-choice MCQ

A memoize helper caches results in a closed-over Map. Given this call sequence, how many times does `compute` actually run?

function memoize(fn) { const cache = new Map(); return (n) => { if (cache.has(n)) return cache.get(n); const r = fn(n); cache.set(n, r); return r; }; } let calls = 0; const compute = memoize((n) => { calls++; return n * 2; }); compute(2); compute(2); compute(3); compute(2); console.log(calls);