DEV Community

Michael Bazile
Michael Bazile

Posted on • Updated on

Memoization

Hi there, are you perhaps looking for a blog that can explain what the heck memoization is? Well my friend lucky for you, you've come to the right place. My name is Michael Bazile, I'm an aspiring software engineer from New Orleans. I currently attend Operation Spark, and this is my very first blog ever, so come and join me on this educational journey together.

To get things started, I'll just give a basic overview of what memoization is. The basic concept behind memoization is the idea of saving the results of function calls inside of a cache so if that same input is given to the memoize function again, the result is already saved on the cache so the function in return does not have to do the entire computation of that function.

Caching means storing the copies of frequently used data in cache memory so that we can access it faster. Cache memory is faster to access.

This is amazing for reducing the time in which how long a computation has to take in order to get a result. As a current student myself, one major concept I've learned about is time complexity. Meaning how much, on average, does it take to start and finish an algorithm. Memoization is an awesome way to so do, let me show you how.

let memo = func => {
  //cache variable, to store all of your key/value pairs
  let cache = {};

  //return a function
  return (...args) => {
    //make keys for cache
    let key = JSON.stringify(args);

    //check if key is not on cache
    if (!cache[key]) {
      cache[key] = func(...args);
    }
    //if key
    return cache[key];
  }
Enter fullscreen mode Exit fullscreen mode

That was an example of a basic memoize function. As you can see on line one, memoize functions will always take in a function as its first parameter. Moving down to line two is where the cache (object) is declared and saved into a variable. This is the cache that will be saving all the expensive function calls each time that memo function is called. Moving down to line six, the memo function must return a function as well as take in a function. Inside that return function is where all of the interaction with the cache will be. On line eight is where we start to store key value pairs. Any arguments that is passed into the anonymous function will be turned into a string using the JSON.stringify() method. Lines eleven and twelve and fifteen is where it all happens.

On each function call, the memo function will check the cache to see if that key is stored on the cache, if it is, the memo function will simply return the value on that key. If it is not, then the func parameter will be called on the arguments and will be stored on the cache at that key. When the size of your input data increases, having a memoize function to decrease the time it takes to get information will better improve the performance of your algorithm as a whole.

That was a pretty thorough and detailed explanation, but that's how I learned to understand how memoization works by breaking down what the function is doing bit by bit. With this implementation, the time it takes your functions to run will drastically decrease and in turn better optimize your functions.

So in conclusion, memoization is a great tool to optimize function performance by reducing time it takes to run. Memoization does this by saving the results of function calls on a cache (object) where the key is the input and the value is the result of the function call. So with this newly gained knowledge, I hope you use a memo function to memorize some of your function calls in the near future.

Top comments (0)