VOOZH about

URL: https://www.geeksforgeeks.org/javascript/how-to-write-a-simple-code-of-memoization-function-in-javascript/

⇱ How to write a simple code of Memoization function in JavaScript ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to write a simple code of Memoization function in JavaScript ?

Last Updated : 23 Jul, 2025

Memoization is a programming technique that we used to speed up functions and it can be used to do whenever we have an expensive function ( takes a long time to execute). It relies on the idea of cache {}. A cache is just a plain object. It reduces redundant function expression calls.

Let's understand how to write a  simpler code of the Memoization function in JS.

Example 1: In this example, we will see the inefficient way of writing a function that takes more time to compute 

Output:

👁 Image

For this reason, to compute operation in less time for large, we use memoization.

There are two ways to do memoization:

  • Function memoization
  • Cache memoization

Slow function which takes a large time to compute using cache memoization.

Example 2: This example is used to find the Square of number


Output
2500000000
25000000
250000

Uses in Dynamic programming using cache memoization.

Example 3: This example is used to print Fibonacci sequence


Output
354224848179262000000

Using memoizer i.e. it takes a function and then it has to return a function.

Example 4: This example shows memoization in JavaScript

Output:

1250000000
Comment