Memoization In JavaScript
Tags: javascript, webdev, programming, beginners
Published: 2022-07-19 08:00:54


Memoization is a performance optimization technique used to speed up programs by storing the results of expensive function calls and returning the cached results when the same inputs occur again.

In JavaScript, memoization can be used to speed up recursive functions that call the same values multiple times. By storing the results of the function calls, we can avoid having to recalculate them each time, which can lead to significant performance gains.

For example, consider the following recursive function that calculates the nth Fibonacci number:

```jsx
function fib(n) {
  if (n <= 1) {
    return n;
  }
    return fib(n - 1) + fib(n - 2);
 }
```

This function works by recursively calling itself with different values for `n`. The first time it's called, it will calculate the Fibonacci numbers for `n = 0` and `n = 1`. The second time it's called, it will calculate the Fibonacci numbers for `n = 1` and `n = 2`. And so on.

Each time the function is called, it must recalculate the Fibonacci numbers for `n - 1` and `n - 2`. However, many of these values have already been calculated in previous function calls. For example, when calculating the Fibonacci number for n = 5, the function must calculate the values for `n = 3` and `n = 4` even though they were already calculated when `n = 4` was calculated.

This calculation is wasted effort because the values don't change between function calls. We can avoid this wasted effort by memoizing the results of the function calls. Memoization is a technique for caching the results of function calls and returning the cached results when the same inputs occur again.

We can memoize the fib function by adding a cache as a global variable:

```jsx
var cache = {};

function fib(n) {
   if (n <= 1) {
     return n;
   }

   if (!(n in cache)) {
     cache[n] = fib(n - 1) + fib(n - 2);
   }

  return cache[n];
}
```

Now, when the function is called, it will first check to see if the result is already cached. If it is, it will return the cached result. If it's not, it will calculate the result and cache it before returning it.

This technique can be used to speed up any recursive function that calls the same values multiple times. However, it's important to note that memoization only provides performance gains if the function is called multiple times with the same inputs. If the function is only called once, or if it's called with different inputs each time, memoization will actually slow down the program

Some other common use cases for memoization include caching the results of database queries, API calls, and computationally expensive functions..

One potential pitfall of memoization is that it can use up a lot of memory if the function is called with a large number of different inputs. In our Fibonacci example, the cache could potentially grow to contain the results of every Fibonacci calculation from `0`to `n`. For very large values of `n`, this could use up a significant amount of memory.

Another potential issue is that memoized functions can be difficult to debug because the cached results are stored outside of the function. This can make it hard to track the flow of execution through the code.

Overall, memoization is a powerful performance optimization technique that can speed up recursive functions by storing the results of previous function calls. However, it's important to use memoization wisely to avoid potential memory and debugging issues.

Another example of memoization is to use a closure to create a cache that is available to the function being memoized:

```jsx
var memoize = function(fn) {
  var cache = {};

  return function(x) {

    if (cache[x] === undefined) {
      cache[x] = fn(x);
     }

    return cache[x];
   };
};
```

This implementation can be used to memoize any function, as long as it only takes a single argument. To use it, simply wrap the function to be memoized with a call to memoize:

```jsx
var factorial = memoize(function(n) {
  if (n === 0) {
   return 1;
   }
   return n * factorial(n - 1);
});
```

This implementation has a number of advantages. First, it is very simple and easy to understand. Second, it is completely general, meaning that it can be used to memoize any function, no matter how complex.

However, this implementation also has a few drawbacks. First, it only works for functions that take a single argument. Second, it is not very efficient, because it requires the function to be memoized to be called twice in order to retrieve the cached value.

A more efficient implementation of memoization can be achieved by using an object as a cache, and using the Function.prototype.apply method to call the memoized function with the arguments passed in:

```jsx
var memoize = function(fn) {
  var cache = {};
  
 return function() {
     var key = JSON.stringify(arguments);
     
     if (cache[key] === undefined) {
        cache[key] = fn.apply(this, arguments);
     }
		
		 return cache[key];
		};
};
```

This implementation can be used to memoize any function, no matter how many arguments it takes. To use it, simply wrap the function to be memoized with a call to memoize:

```jsx
var factorial = memoize(function(n) {
  if (n === 0) {
   return 1;
   }
   return n * factorial(n - 1);
});
```

This implementation is more efficient than the previous one because it only requires the function to be memoized to be called once. In addition, it can be used to memoize any function, no matter how many arguments it takes.

The disadvantage of this implementation is that it is more complex, and therefore more difficult to understand. In addition, it is not possible to use the same cache for multiple functions, because the cache key is specific to the arguments passed to the function.

A final way to implement memoization in JavaScript is to use the Map data structure:

```jsx
var memoize = function(fn) {

var cache = new Map();
  
return function(x) {
    if (!cache.has(x)) {
				cache.set(x, fn(x));
		}

    return cache.get(x);
  };
};
```

### Conclusion

As you can see, memoization is a simple technique that can be used to improve the performance of JavaScript programs. By memoizing expensive function calls, we can avoid having to perform the computations multiple times, which can give our program a significant performance boost.

---