Member-only story
Understanding JavaScript/TypeScript Memoization
A JavaScript and TypeScript tutorial

What Is Memoization?
The definition of memoization from Wikipedia is the following:
“In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.” — Wikipedia
Memoization is a programming technique which allows you to reduce the function’s time cost for space cost. That is, the functions which are memoized gain speed for a higher use of memory space.
The memoization can only be used in pure functions, so the first point to know is that it is a pure function.
In the following animation, you can see the final result of applied memoization in our code.

What is a pure function?
A pure function is a function that meets the following criteria:
It is a function that always returns the same result when the arguments are the same. For example, the following functions are impure:
- Functions which use random numbers.
- Functions which use date time as seed to generate the result.
It is a function that does not produce side-effects in the application:
- Data mutation or change application state.
- Network request.
- Database or file request.
- Obtaining user input.
- Querying the DOM.
Benefits
Pure functions are used in web development because of several benefits. Although, pure functions are not only used in web development. Well, the main pure function benefits are:
- Your code is more declarative, which is focused on what must be done and not on how to do it. Also, the functions are focused on how different inputs are related to outputs.