Learn the vocabulary of caching a pure function's return value keyed by its input arguments.
0 / 5 completed
1 / 5
At standup, a dev mentions caching a pure function's return value keyed by its input arguments, so calling it again with the same arguments returns the cached result instead of recomputing it. What is this technique called?
Memoization caches a pure function's return value keyed by its input arguments, so a later call with the exact same arguments returns the cached result instantly instead of redoing the same computation. Garbage collection is an unrelated memory-reclamation mechanism, not a caching technique for function results. This technique works specifically because a pure function always returns the same output for the same input, which is what makes the cached result safe to reuse indefinitely.
2 / 5
During a design review, the team confirms the function being memoized has no side effects and its return value depends only on its arguments, never on external mutable state. Which capability does this property enable?
This property enables safely reusing a cached result for identical arguments, since a pure function's output can never differ from one call to the next given the same input, making the cached value permanently valid to reuse. A function whose return value depends on external mutable state could return a different result for the same arguments at different times, which would make blindly reusing a cached value incorrect. This purity requirement is exactly why memoization is applied selectively, to functions actually known to behave this way, rather than universally to every function in a codebase.
3 / 5
In a code review, a dev notices a memoization cache keyed by a function's arguments has no maximum size and no eviction policy, so every unique set of arguments ever seen stays cached forever. What does this represent?
This is an unbounded memoization cache risking unbounded memory growth, since if the memoized function is ever called with a large or effectively unlimited variety of distinct arguments, the cache keeps growing by one entry per unique input forever, with nothing ever evicted. A deadlock is an unrelated concurrency concept about blocked threads. This is exactly why a production memoization cache typically needs a maximum size or an eviction policy, unless the function's input space is genuinely known to be small and bounded.
4 / 5
An incident report shows a service's memory usage grew unbounded over time because a memoization cache keyed on a user-supplied search query string kept every distinct query ever seen cached forever, and the space of possible queries was effectively unlimited. What practice would prevent this?
Adding a maximum size and an eviction policy, such as least-recently-used, to the memoization cache ensures old, rarely reused entries are actually released once the cache reaches its bound, which is exactly what was missing in this incident's unbounded query cache. Continuing to memoize every distinct query forever with no bound at all is precisely the pattern that let memory grow without limit. This bounded, eviction-aware design is a standard requirement for memoizing any function whose input space isn't genuinely small and fixed.
5 / 5
During a PR review, a teammate asks why the team only memoizes functions confirmed to be pure, with no side effects and no dependency on external mutable state, instead of applying memoization broadly to any expensive function. What is the reasoning?
Memoizing a function whose output can change for the same arguments, because it reads some external mutable state, risks returning a stale cached result that no longer reflects that state's current value, silently producing an incorrect answer the caller has no way to detect. A pure function's cached result, by contrast, is always correct to reuse indefinitely, since its output is mathematically tied only to its input. The tradeoff is that this restricts memoization's applicability to a subset of functions, requiring the team to actually verify purity before caching, rather than applying the optimization indiscriminately.