Introduction to Memoization in JavaScript with Examples

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.

For this reason, we have a concept called memoization in javascript which helps us to rescue from such expensive computations.

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.

Example :

Memoization in Javascript Example

In the above example we are calculating the first five natural number and trying to reduce the time of the function by using the concept of memoization in javascript. We are generally running the function for the first time where it calculates the sum. And after that whenever we call the same function with the same value then we are giving the previous output to the user without running the function multiple times as we have stored the sum already in a cache .

Output:

Memoization Output

Conclusion

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.

The concept of memoization has been applied in different libraries of javascript like:

 

 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

0FansLike
3,742FollowersFollow
19,400SubscribersSubscribe
- Advertisement -spot_img

Latest Articles