Memoization
ELI5 — The Vibe Check
Teaching your code to remember answers it already calculated. If someone asks 'what's 47 × 83?' and you worked it out once, you write down the answer. Next time they ask, you just look it up instead of doing the math again. Trade memory for speed.
Real Talk
Memoization is an optimization technique that caches the results of expensive function calls and returns the cached result when the same inputs occur again. It's a form of dynamic programming. In React, useMemo and React.memo are common memoization tools. The tradeoff is increased memory usage for reduced computation.
Show Me The Code
// Without memoization
function fib(n) {
if (n <= 1) return n
return fib(n - 1) + fib(n - 2) // 🐌 exponential
}
// With memoization
const memo = {}
function fib(n) {
if (n in memo) return memo[n]
if (n <= 1) return n
memo[n] = fib(n - 1) + fib(n - 2)
return memo[n] // ⚡ linear
}
When You'll Hear This
"Memoize that computation — it's recalculating the same thing 50 times." / "useMemo prevents unnecessary re-renders in React."
Related Terms
Caching
Caching is saving the result of a slow operation so you can reuse it quickly next time.
Pure Function
A pure function is the well-behaved kid of programming.
React
React is a JavaScript library from Meta for building UIs out of components.