Memoization
Also called: memoize, memo, function cache.
A function remembers its answer for each input it has seen. The first call with an input does the slow work and stores the answer in a small map; the next call with the same input returns the stored answer at once. It only works for functions that always give the same answer for the same input.
A report with 6 rows calls priceRules(version) once per row. The rows share only 2 versions. Run it, then turn on Memoize and run it again.
Computations: 0 · Memo lookups: 0 · Time: about 0 ms
Nothing run yet. Memoize is off.
Say it in a prompt
Memoize priceRules(formVersion) inside the report job: keep a Map from version to result for one report run, compute on the first call for a version and return the stored result after that. The report has 6,000 rows but only 12 versions, so expect 12 computations, not 6,000. Clear the map when the run ends. Vague vs precise prompt
Vague prompt
the report is slow, make it faster Typical resultAdds Redis in front of the whole report with a 1-hour TTL. The first run is still slow, and people see an old report for an hour.
Precise prompt
Memoize priceRules(formVersion) for one report run with a Map keyed by version: compute once per version, reuse the result for every other row, and clear the map when the run ends. Typical result6,000 rows with 12 versions do 12 slow computations instead of 6,000. Nothing extra to run or expire, and the next run starts fresh.
Seen on
- Python docs: functools.cache and lru_cache wrap a function so calls with the same arguments return the saved result; the docs call it memoizing and say it saves time for expensive functions.
- React docs: useMemo caches the result of a calculation between re-renders and only runs it again when one of its inputs changes.
You might describe it as
- a function that remembers its answers
- don't compute the same thing twice in one run
- same input, so reuse the result from last time
Not to be confused with
- Cache-aside
Memoization remembers a function's answers inside one running program, keyed by its inputs; cache-aside keeps shared data in a cache like Redis, with a TTL, for every server.