DEV Community

Nashmeyah
Nashmeyah

Posted on

JavaScript: Memoization

Hey Guyz! I wanted to write a quick little blog about the memoization topic in JavaScript. At the start of my learning, the concept was so confusing to me(as was may other things lol), so I am writing this blog for you as well as for me solely for the purpose referring back to it in the future. Cool, lets get into it!

What is Memoization?

Memoization is cache return value of a function based on specific parameters. "When we input the same value into our memoized function, it returns the value stored in the cache instead of running the function again, thus boosting performance. No longer does your program have to recalculate every number to get a result."(Medium, codesmith Jan 17, 2018)

Memoization is essentially creating an object, checking for values matching the parameters, then storing new key-values that aren't available. Breaking down complex problems into tinier problems then storing it in a table(array, hash, and map)

function memoizeMultBy2() {
  const cache = {}
  return function (num) {
    if (num in cache) {
      return cache(num)
    } else {
      cache[num] = num * 2
      console.log("Loading....")
      return cache[num]
    }
  }
}

const memoize = memoizeMultBy2()
console.log(memoize(2))
Enter fullscreen mode Exit fullscreen mode

I followed this algorithm by watching an explanation by youtuber Adam Coder. Summarizing the algorithm, we created a cache object, within the function we are also returning an function, we initialized and declared the variable memoize and invoked it so we are able to pass in a parameter. The first time we run this algorithm, we will get "Loading...." then the answer 4, when this algorithm is run again, the num parameter has already been cached so it displays the num without running through the algorithm again saving run time.

I hope this made sense and helped you, please let me know if there is anything you think I should clarify. Keep Smiling and enjoy life!

Discussion (1)

Collapse
markfilan profile image
markfilan

Memoization is an optimization technique that allows an increase in the performance of a program by storing the results of some expensive function so that we don’t need to call that function when the same inputs are given. It does this by storing computation results in cache, and retrieving that same information from the cache the next time it's needed instead of computing it again. The concept of Memoization in Javascript is backed by two main sub-concepts: Closure and High order function. In JavaScript, closures are generated every time a function is created and it allows you access to the domain of the outer function from the inner function. A high order function accepts another function as an argument or returns a function as its output.

This technique is specially useful when it comes to dynamic programming . A memoized function is usually faster because if the function is called subsequently with the previous value(s), then instead of executing the function, would be fetching the result from the cache .

net-informations.com/js/default.htm