![]() |
VOOZH | about |
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:
For this reason, to compute operation in less time for large, we use memoization.
There are two ways to do 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
2500000000 25000000 250000
Uses in Dynamic programming using cache memoization.
Example 3: This example is used to print Fibonacci sequence
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