Home > Article > Web Front-end > Explain the implementation of memory assistance function
Memoization is an auxiliary function, or we can say it is a technique that improves program efficiency by tracking the values that a function has calculated in the past. In this article, we will discuss the Memoization Helper functionality through different examples and discuss all the examples in detail so that we can better understand Memoization.
Now let us discuss the Memoization helper functions in depth in the following sections and see their implementation and explanation.
Memoization is a programming technique used to improve the time complexity and space complexity of a program by keeping track of values that a function has calculated in the past. By saving the results of function calls in cache, programs become more efficient. We often waste time by repeatedly running functions with the same parameters that were previously calculated. We can then cache the calculated value and return it when the function is called with the same parameters.
Here we'll explore a number of examples and explanations to help you better understand memory helper functions.
Let us see how the memory helper function works through this example, in this example we will discuss the code, output and explanation to better understand 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 the add function, passing in two parameters num1 and num2 to perform the addition of integers num1 and num2. In this function, we run a for loop, after which we have to return the sum of two integers.
In this example, we called the addition function, but our function takes some time because of the for loop. We call the function again and again with the same parameters. So if we use memory by storing the added value so that we can save time, then we will return the cached value. We do not need to calculate additional values for the same parameters.
Let's see with the help of code and explanation how long it takes our function to determine the value of add (5,4) -
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 took 14.441ms to add the integers 5 and 4.
By using memoization technology, we can cache the already calculated value and then return it when calling the function with the same parameters, thereby improving the efficiency of the function.
Now let's discuss how to use memoization techniques to shorten the time required to repeatedly execute a function with the same parameters.
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 required to complete a task may change.
In this example, we cache the value previously calculated using the memory function. When we initially use func(4,5), the arguments are first converted to string form and then saved in the object "storage" along with the calculated value.
Additionally, when the function is called with the same parameters, it first determines whether it already exists in the object "storage". If it has already been calculated, it will not be calculated again, but only the value contained in the object's "storage" will be returned.
As you can see from the output, adding 5 and 4 takes less time every time you use the function with the same parameters.
It takes time every time -
98.885 ms 83.375 ms 13.071 ms
So, it is obvious from the output that the memoization technique helps reduce the time we spend each time repeatedly calling a function with the same parameters.
Let’s discuss another example of the Fibonacci sequence memory helper function.
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");
If all steps are performed without the help of memory techniques, the execution time of the Fibonacci sequence will increase exponentially. By storing previous results, we can get predefined results, reduce further checking of the calculation results, and steps can be taken to linearize.
In this article, we learned that Memoization is a helper function or a technique that improves the efficiency of a program by keeping track of the values that a function has calculated in the past. By saving the results of function calls in cache, programs become more efficient. We can then cache the calculated value and return it when the function is called with the same parameters.
The above is the detailed content of Explain the implementation of memory assistance function. For more information, please follow other related articles on the PHP Chinese website!