Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumJavaScriptmemoizationSingle-choice MCQ

This memoizer is meant to cache per-instance, but it shares one cache across all instances. What is the fix that keeps memoization correct per instance?

class Calc { constructor(base) { this.base = base; } } const cache = new Map(); Calc.prototype.compute = function (n) { if (cache.has(n)) return cache.get(n); const v = this.base + n; cache.set(n, v); return v; }; const a = new Calc(10), b = new Calc(100); console.log(a.compute(5), b.compute(5));