Search⌘ K
AI Features

A Small Aside: Memoization

Explore the concept of memoization in functional programming and how it leverages pure functions to cache results for repeated inputs. Understand how to implement memoization in TypeScript using fp-ts libraries and apply these techniques to optimize performance in compute-heavy functions.

In an earlier chapter, we briefly discussed how FP could be less performant than other styles of programming in some situations. Besides the advantages of high-level programming when it comes to optimizations that an engine might perform without us even knowing, one specific and powerful tool is available to the functional programmer: memoization.

Memoization relation with pure functions

Memoization gains its power from pure functions. Because we know that a pure function’s result is only dependent on what we pass in, we can deduce that if we calculate the result for a given set of parameters, we can return that ...