DEV Community

Kerisnarendra
Kerisnarendra

Posted on • Updated on

Memoization in JavaScript: The need for speed

Memoization is a technique used in programming to optimize the performance of frequently executed functions. It involves caching the results of a function call, so that the same results can be returned for subsequent calls with the same arguments, rather than recalculating them each time.

Memoization is based on the idea that if a function is called with the same arguments multiple times, it will return the same result each time. Therefore, it makes sense to cache the result of the first function call and return it for all subsequent calls with the same arguments.

The process of memoization involves creating a cache object, which is used to store the results of function calls. When a function is called with a set of arguments, the cache object is checked to see if the result for those arguments already exists. If it does, the cached result is returned; otherwise, the function is executed, and the result is stored in the cache object for future use.

Memoization can be particularly useful for functions that are computationally expensive or have complex logic. By caching the result of the first call, subsequent calls with the same arguments can be executed quickly and without the need to repeat the same calculations.

In Node.js, memoization can be implemented using a variety of techniques. One common approach is to use a JavaScript object to store the cached results. Here's an example of a simple memoization function:

function memoize(func) {
  const cache = {};
  return function(...args) {
    const key = JSON.stringify(args);
    if (cache[key]) {
      return cache[key];
    }
    const result = func.apply(this, args);
    cache[key] = result;
    return result;
  };
}
Enter fullscreen mode Exit fullscreen mode

This function takes a function as an argument and returns a new function that wraps the original function with memoization. The cache object is created as an empty JavaScript object, and a new function is returned that will be used to execute the memoized function.

The memoized function takes a variable number of arguments using the spread syntax ...args. These arguments are converted into a string using the JSON.stringify() method, which is used as the key for the cache object. If the key exists in the cache, the cached result is returned. If it doesn't, the original function is executed using the apply() method, and the result is stored in the cache object for future use.

Here's an example of how you can use the memoization function:

function fibonacci(n) {
  if (n <= 1) {
    return n;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
}

const memoizedFibonacci = memoize(fibonacci);

console.log(memoizedFibonacci(10)); // Output: 55
console.log(memoizedFibonacci(10)); // Output: 55 (cached result)
Enter fullscreen mode Exit fullscreen mode

In this example, the fibonacci() function calculates the Fibonacci sequence for a given number. The memoize() function is used to create a new function memoizedFibonacci that wraps the fibonacci() function with memoization. The memoizedFibonacci() function is then called twice with the same argument, and the result is returned from the cache object on the second call.

Memoization is a powerful technique that can greatly improve the performance of your Node.js applications. By caching the results of frequently executed functions, you can avoid repeating expensive calculations and reduce the overall processing time.

Memoization is one of effective techniques for optimizing the performance of functions that execute SQL queries. By caching the results of previous queries, subsequent calls with the same arguments can be served quickly without having to re-execute the query. Another approach for improving performance is to use materialized tables, which store the results of a query in a physical table, so that subsequent queries can retrieve the pre-calculated results directly from the table. Are you interested in materialized tables topic? I discussed it in another post.

Top comments (0)