Memoization in JavaScript is an optimisation that caches the result of a function call, keyed by its arguments, so repeated calls with the same arguments return the stored value instead of recomputing it. It trades memory for speed and only works safely for pure functions. This guide builds on pure functions, closures and higher-order functions.
The classic demonstration is a recursive Fibonacci function that drops from exponential time to linear time once results are cached.
A memoized function keeps a lookup table. On each call it checks the table: if the arguments were seen before, it returns the saved result; otherwise it computes, stores and returns the new result.
Memoization is correct only when the function is pure: the same input must always give the same output, and calling it must have no side effects. If the function reads the clock, a random number or mutable outside state, a cached value can be wrong.
It pays off when the function is expensive, is called many times with repeating inputs, and has a limited set of distinct inputs.
The helper below wraps any function of one primitive argument using a Map.
Use cache.has(arg) rather than checking whether the value is truthy. Otherwise results such as 0, false or "" would be recomputed every time. The cache lives in a closure, so each memoized function has its own private store.
Naive recursive Fibonacci recomputes the same sub-problems over and over, giving exponential time.
To benefit from memoization, the recursive calls must go through the memoized version:
Each value from 0 to 50 is computed once, so the work is linear. If you memoize a copy but the function still calls its original unwrapped name, you get no speed-up. Very large n can still overflow the call stack because of recursion depth, and results above Number.MAX_SAFE_INTEGER need BigInt.
A Map compares keys by identity for objects and by value for primitives, so you need a way to turn several arguments into one key. A common approach is JSON.stringify.
JSON.stringify has limits. It ignores undefined and functions, throws on circular structures and on BigInt, and treats {a:1,b:2} and {b:2,a:1} as different keys. For arguments that are objects, a WeakMap keyed by the object avoids this, as shown next.
When the argument is an object, use a WeakMap. Entries disappear automatically when the object is garbage collected, which prevents memory leaks.
The cache is keyed by identity, so a different object with the same contents is a cache miss. If you mutate order after the first call, the cached total becomes stale, which is another reason to treat data as immutable.
Map never forgets. For functions with many distinct inputs, limit the size, for example by removing the oldest entry when the cache passes a maximum (a simple least-recently-used policy).Map keeps insertion order, so the first key is always the least recently used one in this pattern.
Memoization is caching applied specifically to function results, keyed by the function's arguments, and handled inside the function wrapper. Caching is the broader idea and also covers things like HTTP caches and database caches.
Only pure functions are safe. A function with side effects will skip those effects on a cache hit, and a function that depends on changing outside state will return outdated results.
It uses the same idea but remembers only the most recent result for a given set of dependencies in a component, not every earlier input. It is meant as a performance hint, not a guarantee.
They are closely related. Memoization is the top-down form, where you add a cache to a recursive solution. Dynamic programming often means the bottom-up form, which fills a table in order.
Memoization stores the result of a pure function for each distinct input so repeat calls are instant. Build it with a closure and a Map, use has instead of truthiness checks, and route recursive calls through the memoized function. Use WeakMap for object arguments, bound the cache size for open-ended inputs, and never memoize functions that depend on changing state.