DEV Community

Jack Cole
Jack Cole

Posted on

Learning Memoization in Javascript Part 2

After doing some research on how to implement memoization in a function with multiple arguments I realized this is a pretty unusual situation that will rarely occur...

In my last post I went over implementing memoization in javascript using the fibonacci sequence as an example. However, in that example our function only utilized a single argument. But how do we memoize when a function needs to track multiple arguments?

Implementation

In my example I'm going to use a nested map object. The map will have multiple dimensions and store the fibonacci data in the dimension that matches the specific method call. Remember, this is just an example displaying how we would go about doing this.

Also going on a short tangent, notice how I use map objects instead of a regular hash. Javascript has a map object, learn to use it! You can read more about that here.

Memoization Multiple Functions

Thanks for reading! I know that this is a pretty niche situation but memoization is a good skill to know and could come in handy in the future! The code for this post and my last post can be found here.

Top comments (1)

Collapse
 
rtivital profile image
Vitaly Rtishchev