What is Memoization in JavaScript?

Introduction to Memoization in JavaScript.

ยท

3 min read

What is Memoization in JavaScript?

Memoization 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.

Memoizing in simple terms means memorizing or storing in memory. A memoized function is usually faster because if the function is called subsequently with the previous value(s), then instead of executing the function, we would be fetching the result from the cache.

How Does Memoization Work?

Memoization works on mainly two concepts In JavaScript.

  • Closure

    • The closure is a combination of a function wrapped together (enclosed) with references to the corresponding state (the lexical environment).

    • In other terms, the Closure allows you access to the domain of the outer function from the inner function.

    • In JavaScript, closures are generated every time a function is created, at the time a function is created.

  • High-order Function

    • A high-order function accepts another function as an argument or returns a function as its output.

When should we use Memoization?

  • We can use Memoization in recursive functions.

  • To memoize a function, it should be pure so that return values are the same for the same inputs every time.

  • Memoization is a trade-off between added space and added speed and is thus only significant for functions having a limited input range so that cached values can be made use of more frequently.

Is Memoization the same as caching?

Yes, kind of. Memoization is actually a specific type of caching. While caching can refer in general to any storing technique (like HTTP caching) for future use, memoizing specifically involves caching the return values of a function.

Let's try to understand Memoization with an example.

function getSquare(){
  let cache = {};

  return function (num){
    if(num in cache){
    console.log(`The square of ${num} is ${cache[num]}. It is present in the cache.`);
      return;
  };

  cache[num] = num*num;
  console.log(`The square of ${num} is ${cache[num]}. It isn't present in the cache.`);
  return;
  };
};

let _getSquare = getSquare();
_getSquare(2);
_getSquare(3);
_getSquare(2);
_getSquare(3);

Output:

The square of 2 is 4. It isn't present in the cache.
The square of 3 is 9. It isn't present in the cache.
The square of 2 is 4. It is present in the cache.
The square of 3 is 9. It is present in the cache.

Conclusion:

  • Memoization is an optimization technique, to reduce the complexity of the application.

  • We store the values that were calculated previously.

  • Memoization mainly depends on two concepts:

    • Closure

      • The closure is a combination of a function enclosed with its references to the state.
    • High order function

      • A higher-order function is a type of function that operates on other functions, they either take other functions as arguments or return them.
  • Only pure function can be used for memorization.

Happy Codings ๐Ÿง‘๐Ÿผโ€๐Ÿ’ป

ย