Memoization in JS
Memoization - Memoization is an optimization technique that can be used to reduce time-consuming calculations by saving previous input in the cache and returning the result from it.
example
let sum = 0;
const calc = (n) => {
for (let i = 0; i <= n; i++) {
sum += i;
}
return sum;
}
const memoize = (fun) => {
let cache = {};
return function (...args) {
let n = args[0];
if (n in cache) {
return cache[n];
}
else {
let result = fun(n);
cache[n] = result;
return result;
}
}
}
let total = memoize(calc);
console.time();
console.log(total(5));
console.timeEnd();
console.time();
console.log(total(5));
console.timeEnd();
output ->
Now we can see for 1st-time of input 5 it's taking 6.306ms time to execute because the for loop is running 5 times and it took the 6.306ms.
And after returning the sum of the 1st-time input number we stored that also in cache object.
Then if we pass that number again which is present inside the catch object we just return that, didn't call the calc function again. So it took just 0.333ms