What is memoization — and why a cache keyed by an object never hits

You ask an AI to speed up a slow calculation. It wraps the function in memoize and says: "the result is cached now." You measure it — nothing changed. The function still computes everything from scratch on every call.
The code isn't broken. The cache just never found a match. Let's see why — and along the way figure out what memoization is and when it actually helps.
What memoization is
Memoization is when a function remembers its answers. Same arguments come in — it doesn't recompute, it hands back the saved result.
Picture a student solving problems. The first time, they honestly work out "17 × 23" on paper. They jot the answer in a notebook. Next time they see the same problem, they just check the notebook.
The notebook is the cache. Memoization is a special case of caching: you cache a function's answers, and its arguments are the key.
How it works under the hood
Here's memoization in eight lines. Pretty much any AI writes it almost word for word:
function memoize(fn) {
const cache = new Map()
return (arg) => {
if (cache.has(arg)) return cache.get(arg) // already computed — return it
const result = fn(arg) // compute for the first time
cache.set(arg, result) // write it in the notebook
return result
}
}
const slowSquare = (n) => { /* slow calculation */ return n * n }
const fastSquare = memoize(slowSquare)
fastSquare(9) // computes
fastSquare(9) // instantly from the cache
Notice: memoize returns a new function, and cache lives inside it between calls. That's scope at work: the inner function remembers the outer one's variables.
The trap: an object as the argument
Now back to the mystery from the start. Look:
const getTotal = memoize((cart) => heavyCalc(cart))
getTotal({ id: 1 }) // computes
getTotal({ id: 1 }) // computes again!
To a human, these are the same cart. To JavaScript, they're two different objects. Each { id: 1 } creates a new object in memory. Map compares objects by reference, not by content. Different references — no match.
We checked on Node 22: both calls are cache misses. No error, no warning. The cache just piles up garbage and doesn't help.
How to fix it:
- Pass a primitive. Instead of the whole cart, pass its
id(a number or a string). Strings and numbers are compared by value. - Build the key yourself. For example,
JSON.stringify(cart)turns the object into a string. It works, but serializing costs time too. - Pass the same object. If the cart lives in one variable and isn't recreated, the reference stays the same and the cache hits.
When memoization hurts
Memoization is only safe for pure functions — ones that always give the same answer for the same arguments.
Here's a function you must not memoize:
const greeting = memoize((name) => `Hi, ${name}! It's ${new Date().getHours()} o'clock.`)
The first call at 9 a.m. remembers "9 o'clock". In the evening the function still says "9 o'clock". The answer froze. Same with Math.random(), a server request or a database read — the data changes, and the cache has no idea.
The second problem is memory. Our Map is never cleared. If the function gets called with a million different arguments, the cache bloats and slows down the whole app. Serious versions cap the cache: keep, say, the last 100 answers.
Where you'll run into memoization
- React:
useMemoremembers a calculation result,React.memoa whole component,useCallbacka function. Same trap there: pass an object created right in the markup, and it's new on every render, so the memo never hits. - Recursion: the classic is Fibonacci numbers. Without memoization,
fib(40)calls the function hundreds of millions of times. With it — about forty real computations; the rest comes from the cache. - Expensive AI calls: there's no need to send the same question to a model twice. Same idea, the cache just lives on the server.
Should I put useMemo everywhere?
No. The cache itself costs time and memory. For fast calculations like adding two numbers, memoization is slower than just computing. Reach for it when a profiler shows a real bottleneck.
How is memoization different from caching?
Caching is the general word: you can cache images, pages, server responses. Memoization is a narrow kind of cache: remembering a specific function's result by its arguments.
Why does AI add memoization so often?
Because it's a common "optimization pattern" in training data. The model drops it in out of habit, without checking whether the function is pure and the arguments are primitives. Ask directly: "is the cache key a primitive? is the function pure?"
speed up this functionThe calcTotal(cart) function is slow. Before adding memoization, check: 1) is the function pure, with no Date, random or requests? 2) is the argument an object that gets recreated on every call? If so, build the cache key from cart.id and cart.updatedAt. Cap the cache at 100 entries.The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





