@Calendee

Lodash Memoize with a Resolver

A while back, I wrote about Lodash Memoize. However, I missed a critical point about it that Pavel Zubkou pointed out.

memoize uses the first argument to create the key to the cache. If the method you want to memoize takes more than one argument, the additional ones would get ignored.

To fix this problem, you can pass a resolver to memoize. The resolver will be responsible for determining the key to use to add to and query the cache.

Here's an example with a resolver:

function add(a, b) {  
  console.log("Add called with ", a, b);
  return a + b;
}

const adder = memoize(add, (...args) => values(args).join("_"));  

The resolver takes all the arguments and concatenates them to create a unique string. Now, anytime that same unique set of arguments is used, memoize wil return the cached result.

Here's are working example: