Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
caching
communitycreator

What is memoization?

Teri Eyenike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Memoization is a specific form of caching used in dynamic programming. Caching is a way to speed our programs and hold some data in an accessible box for later use. It stores the previous result and returns the others instantaneously when we call without delay.

In addition, memoization is an optimization technique in caching that results when the same set of arguments result in the same output and performant web applications.

Fibonacci sequence

Let’s write a function to compute the Fibonacci sequence without memorization.

The Fibonacci sequence represents a list of numbers where each value is the sum of the two previous values.

Console

From the code above, we didn’t cache the execution of our function. The process will be a lot quicker in less time with caching.

Console

From the code snippet above, we created a cache object that the fib() uses to store its output value. Each time the function is called, it first checks whether the input num has been stored previously in the cache object. If it has, it immediately returns the cached value as the program

Exercise

Let’s find the factorial of a given number using the cache execution of a factorial() function.

Console

RELATED TAGS

javascript
caching
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring