Appendix
Appendix A – Not a Classical Memorization
It's very easy to confuse useMemo
with a Memorization used in computer science, as the name indicates.
Memorization is an optimization technique in computer programs, primarily designed to speed up the process by storing the results of expensive operations and returning the cached result if it has been computed under the same condition before. The last part, "under the same condition before," is the part that makes it special.
The Fibonacci sequence is a classical memorization problem. If it's written using a recursive algorithm, it can be very costly; therefore, we tend to use cache storage to store all past calculated values:
const fibs = { 0: 1, 1: 1 } function fib(n) { if (!fibs[n]) { fibs[n] = fibs[n - 1] + fibs[n - 2] } return fibs[n] }
The preceding code gives a specialized fib
function; if you call it sequentially...