As we all know that functions are a vital part of any programming language and as a developer, we use functions quite often in our projects. The best part about functions is that they are reusable and you can call them from any part of the program. Other than that functions are very powerful as they can return other functions or take functions as an argument.
In a large program when a program computation depends upon executing the results of another function then it simply means that whenever we will execute the program it will require us to run and call the functions repeatedly. In general, executing such functions is quite an inefficient way as that needs to do long and heavy computation.
What is Memoization?
Memoization is an optimization technique that can be used for reducing time-consuming calculations by saving the previous input in something called a cache and returning the result from it.
In simple words, we can say that memoization is a technique of caching the results of expensive function calls to speed up the programs by returning the cached result when the same input occurs again.
Importance of Memoization
- It is generally an optimization technique that increases the performance of a program by caching the results of a function call. It generally stores the previous results and then it retrieves the result whenever it is required.
- Let us consider a case where you need to execute a function not one time, not two times but several times then it will be better if you memoize the result of that function. In this way, you need to just memoize the result of the function only once.
When to use Memoization
- We can use the concept of memoization when the function is a pure function. If the function is an impure function then it will return different values when it is executed.
- We can use memoization whenever there is a heavy computation task to perform in a program. Generally whenever a program has high computation tasks then caching the results will generally increase the performance of our program.
- Whenever you are making remote API calls repeatedly then using memoization will save you from making repetitive calls to the server.
Memoization is an optimisation technique that can be applied to any programming language. Generally the goal of memoization is optimise high computation tasks performed by a program.