JS ALGORITHMSChapter 8 · JS Algorithms
Memoization
Memoization stores the result of each call so the same input never gets computed twice. Naive recursive Fibonacci recomputes the same values over and over and its work explodes exponentially; with a cache it becomes linear. A small higher-order memoize function can add caching to any function of one argument.
Worked example
How it reads
slowFib(20)makes more than 20,000 callsmemoizewraps any function with a cachefastFibcalls the memoized version, so every n is computed once

Cloud tip: Memoization only pays off when the same inputs come up again, like overlapping recursive calls.


