- JavaScript
- JavaScript Utility Implementations and Standard Polyfills
- Memoize and Cache Layer
JavaScript Memoization & Caching Layer Guide
In JavaScript, memoization is an optimization technique that caches pure function outputs based on input arguments using closure scope. Custom key resolvers handle complex or order-independent parameters, while bounded LRU caches prevent memory leaks.
Key Learning Objectives
Differentiate pure function memoization from general HTTP and database caching.
Implement closure-based function memoization supporting multiple arguments and custom key resolvers.
Understand argument serialization traps: why JSON.stringify distinguishes argument order and fails on circular objects.
Implement memory-leak resilient caching using WeakMap and LRU (Least Recently Used) capacity bounds.
Identify critical limitations: why functions with side effects, non-deterministic outputs, or async rejections cannot be safely memoized.
The Interview Problem
What is logged to the console when the following memoized functions are invoked, and how do default JSON argument serialization versus custom key resolvers determine cache hit rates?
1function memoize(fn, resolver) {2 const cache = new Map();3 return function (...args) {4 const key = resolver ? resolver(...args) : JSON.stringify(args);5 if (cache.has(key)) {6 return { value: cache.get(key), cached: true };7 }8 const result = fn.apply(this, args);9 cache.set(key, result);10 return { value: result, cached: false };11 };12}1314let calls = 0;15const add = (a, b) => {16 calls++;17 return a + b;18};1920const defaultMem = memoize(add);21const customMem = memoize(add, (a, b) => [a, b].sort().join(','));2223const r1 = defaultMem(1, 2);24const r2 = defaultMem(1, 2);25const r3 = defaultMem(2, 1);26const r4 = customMem(1, 2);27const r5 = customMem(2, 1);2829console.log(calls, r1.cached, r2.cached, r3.cached, r5.cached);
Predict Console Output
Select the option that matches what this code prints to the console:
3 false true false true
2 false true true true
4 false true false false
5 false false false false
V8 Engine Execution Trace
Step 1 of 7 (Line 1)Initializes global environment and allocates memoized wrapper functions.
JavaScript Memoization & Caching Layer Guide Explained
Anatomy of Production-Grade Memoization
Memoization is an optimization technique that caches the return values of pure functions based on their input arguments:
- Closure Scope: The
cache(typically aMap) is encapsulated within the outer function's lexical scope, preserved across calls. - Context Preservation: Always invoke the target function with
fn.apply(this, args)to ensure object methods retain their correctthisbinding. - Key Generation Pitfalls:
JSON.stringify(args)works for simple primitives and JSON-serializable structures, but fails on circular references (TypeError: Converting circular structure to JSON) and ignores functions orundefinedobject properties.- Passing object arguments
{ a: 1, b: 2 }and{ b: 2, a: 1 }produces different strings if key ordering varies, causing false cache misses.
The Memory Leak Problem & LRU Caching
Unbounded memoization caches retain every input and result forever in heap memory. In long-running single-page applications, this causes silent memory exhaustion. The Senior Solution: Implement a Bounded LRU (Least Recently Used) Cache:
function memoizeLRU(fn, capacity = 100) {
const cache = new Map();
return function (...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
const val = cache.get(key);
cache.delete(key);
cache.set(key, val); // Move to most recently used
return val;
}
const result = fn.apply(this, args);
if (cache.size >= capacity) {
const oldestKey = cache.keys().next().value;
cache.delete(oldestKey); // Evict oldest
}
cache.set(key, result);
return result;
};
}When NOT to Memoize
- Impure Functions: Functions reading system state (
Date.now(),Math.random(), DOM elements, or global variables). - Asynchronous Functions with Failures: If a memoized promise rejects, naive caching caches the rejected promise forever, preventing retry recovery.
Common Traps & Mistakes
Assuming `JSON.stringify` handles object references by pointer identity. It serializes structural values, risking collisions and failing on circular objects.
Forgetting to preserve `this` using `.apply(this, args)`. When memoizing class or object methods, losing `this` causes runtime errors.
Memoizing asynchronous functions without handling rejected promises, resulting in permanently cached rejected states.
Failing to bound cache capacity, leading to unbounded memory leaks in enterprise applications.
FAANG Follow-Up Probes
Probe #1
How would you use WeakMap to memoize single-argument functions where the argument is always an object, avoiding memory leaks?
Probe #2
How can you handle asynchronous memoization where duplicate concurrent calls share a single in-flight Promise (request coalescing)?
Probe #3
What is the difference between React.memo (component shallow comparison) and useMemo (dependency array caching)?
JavaScript Memoization & Caching Layer Guide Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Memoize and Cache Layer.
What are the limitations of using JSON.stringify as a default cache key resolver?
JSON.stringify is order-dependent for object keys ({ a: 1, b: 2 } !== { b: 2, a: 1 }), fails on circular references, drops Symbol and function properties, and introduces substantial serialization CPU overhead on large inputs.
How do you prevent unbounded memory growth in a memoized function?
Enforce a maximum cache capacity using an eviction strategy such as LRU (Least Recently Used) or FIFO (First-In, First-Out), deleting the oldest entries when capacity is exceeded.
How can an LRU cache be implemented in JavaScript using native Map?
JavaScript Map preserves insertion order. On cache hits, delete the accessed key and re-insert it (map.delete(k); map.set(k, v)) to make it the most recently used. To evict, delete the first key using map.keys().next().value.
When should WeakMap be used for caching instead of Map?
Use WeakMap when cache keys are exclusively objects and caching should not prevent those objects from being garbage collected when discarded by the rest of the application.
What is the time complexity of cache lookup in a memoized function?
Cache lookup in a Map or hash table runs in O(1) average time. If key serialization is required, key computation runs in O(K) time where K is serialized argument size.
More JavaScript interview questions for experienced developers
