This is one of the most asked interview questions and Memorization in JavaScript refers to the technique of caching and reusing the output of expensive functions that have already been executed with the same arguments. This technique is commonly used in functional programming to improve the performance of complex functions.
In this interview article, we will discuss a simple memorization function that can be used to cache expensive computations in your JavaScript applications.
Implementation:
The memorize function takes a function as an argument and returns a new cached function. This new function calls the original function only when it has not been called with the same arguments before.
function memoize(func) {
const cache = {};
return function (...args) {
const key = JSON.stringify(args);
if (cache[key]) {
console.log("Cached Result")
return cache[key];
}
const result = func.apply(this, args);
cache[key] = result;
return result;
};
}
Explanation:
The memorize function takes a single argument func
, which represents the expensive function we want to memorize. The memorize function creates an empty object called cache
, which is used to store the output of the expensive function.
The memorize function then returns a new function that takes any number of arguments using ES6 rest parameters syntax (...args
). The function first computes a unique key for the current set of arguments using the JSON.stringify()
method. If the cache
object already contains the result associated with the key, the function returns that result instead of calling the expensive function again.
If the key is not found in the cache
object, the expensive function is executed by using the apply
method with the current object (this
) and the set of arguments. The result is then stored in the cache
object against the key and is also returned.
function square(number) {
console.log("Computing...");
return number * number;
}
const memoizedSquare = memoize(square);
console.log(memoizedSquare(4)); // Output: Computing... 16
console.log(memoizedSquare(4)); // Output: Cached Result 16
console.log(memoizedSquare(5)); // Output: Computing... 25
console.log(memoizedSquare(5)); // Output: Cached Result 25
In this example, we have a simple function called square
that takes a number as an input and returns its square. We then create a memorized version of this function by calling the memorize function with the original square
function as an argument.
We then call the memorized function with two different numbers – 4 and 5. The first time we call the function with 4, the memorized function computes and caches the result. The second time we call the function with 4, the cached result is returned immediately without further computation. The same process happens when the function is called for the second time with 5.
Memorization is an effective way to improve the performance of expensive functions in JavaScript. The memorize function we’ve discussed is a simple implementation of the memorization technique that can be used to cache expensive computations in your JavaScript applications.
1 comment
[…] here is one of the most asked machine coding questions on Closures: Implementing Memorization in JavaScript […]