Explain Implement a Memoization Helper Function


Memoization is a helper function, or we can say a technique for improving the efficiency of a program by Keeping track of the values that the function has already calculated in the past. In this article, we will discuss the Memoization Helper function with different examples and discuss all the examples in detail so that we can get a better understanding of Memoization.

Now let’s discuss the Memoization helper function in the deep in the below section and also see their implementation with an explanation.

Introduction to Memoization Helper Function

Memoization is a technique of programming that is used to improve the time complexity and space complexity of a program by Keeping track of the values that the function has already calculated in the past. By saving the outcomes of function calls in a cache, the program becomes more efficient. By repeatedly running the function with the same parameter that has previously been calculated, we frequently waste time. We can then cache the calculated value and just return it when the function is called with the same parameter.

Implementation of a Memoization helper function

Here, we're going to explore a tonne of examples and explanations to help you understand the memoization helper function better.

Example1

Let’s see the working of the memoization helper function with this example in which we will discuss the code, output, and explanation to get a better understanding of the concept −

function add(num1,num2){
   for(let i=0;i<100000;i++){
   }
   return num1+num2;
}
console.log(add(5,4));
console.log(add(5,4));

Here we define function add by passing two parameters in it num1 and num2 for doing the addition of integer num1 and num2. In this function, we have run for loop and after that, we have to return the sum of both the integer.

In this case, we have called the addition function but our function taking time because of for loop. And we are calling the function again and again with the same parameter. So, if there we are using memoization by storing the value of addition so we are able to save time then we will return the cached value. We don't need to calculate the added value for the same parameters.

Example 2

Let's see how long it took our function to determine the value of add (5,4) with the help of code and explanation −

function add(num1,num2){
   for(let i=0;i<100000;i++){
   }
   return num1+num2;
}
console.time("Time taken");
console.log(add(5, 4));
console.timeEnd("Time taken");

Our function taken 14.441ms time to add the integers 5 and 4.

By using the memoization technique, we may improve the efficiency of the function by caching the value that has already been calculated and then returning it when the function is called with the same parameter.

Example 3

Let's now discuss how we can utilise the memoization technique to shorten the time it takes to repeatedly execute a function with the same parameter.

function memoizeFunction(func) {
   let storage = {};
   return function (val1, val2) {
      const val = val1.toString() + val2.toString();
      if (!storage[val]) {
         storage[val] = func(val1, val2);
      }
      return storage[val];
   }
}
function add(num1, num2) {
   for (let i = 0; i < 10000000; i++) {
   }
   return num1 + num2;
}
console.time("First time, time taken");

let func = memoizeFunction(add);
console.log(func(5, 4));
console.timeEnd("First time, time taken");
console.time("Second time, time taken");

func = memoizeFunction(add);
console.log(func(5, 4));
console.timeEnd("Second time, time taken");
console.time("Third time, time taken");

func = memoizeFunction(add);
console.log(func(5, 4));
console.timeEnd("Third time, time taken");

Note − the length of time needed to finish a task can change.

In this instance, we cached the value we earlier calculated using a memoized function. When we initially use func(4,5), the parameter is first converted to string form and then saved within the object 'storage' along with the calculated value.

Additionally, when the function is called with the same parameter, it first determines whether or not it already exists in the object 'storage'. If it is already calculated, it won't do it again and will instead simply return the value that is contained inside the object 'storage'.

As we can see from the output, each time we used the function with the same parameter, the time it took to add 5 and 4 was less.

Time is taken each time −

98.885 ms
83.375 ms
13.071 ms

Therefore, it is evident from the output that the memoization technique assisted in shortening the time each time we repeatedly called the function with the same argument.

Example 4

Let’s discuss another example of a memorization helper function by the Fibonacci number.

function memoizeFunction(func) {
   let storage = {};
   return function (val) {
      const value = val.toString();
      if (!storage[value]) {
      storage[value] = func(val);
      }
      return storage[value];
   }
}
function fibonacci(num) {
   if (num === 0 || num === 1)
   return num;
   else
   return fibonacci(num - 1) + fibonacci(num - 2);
}
console.time("First time, time taken");

let func = memoizeFunction(fibonacci);
console.log(func(6));
console.timeEnd("First time, time taken");
console.time("Second time, time taken");

func = memoizeFunction(fibonacci);
console.log(func(6));
console.timeEnd("Second time, time taken");
console.time("Third time, time taken");

func = memoizeFunction(fibonacci);
console.log(func(6));
console.timeEnd("Third time, time taken");

Fibonacci series take an exponential time to execute if all the steps are performed without the help of memorization technique. By storing the previous results, we can get the pre-defined results to reduce the further checking of the calculated results and can take number of steps to linear.

Conclusion

In this article, we have learned that Memoization is a helper function or a technique for improving the efficiency of a program by Keeping track of the values that the function has already calculated in the past. By saving the outcomes of function calls in a cache, the program becomes more efficient. We can then cache the calculated value and just return it when the function is called with the same parameter.

Updated on: 17-Mar-2023

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements