medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

Better documentation for resolvers? #53

Closed seanohollaren closed 8 years ago

seanohollaren commented 8 years ago

I'm a huge fan of the built-in stats and cache management tools that memoizee offers, but right now I'm totally puzzled by how I'm supposed to use the resolver functionality.

Lodash and Underscore resolvers are very straight forward. For those, you provide a function which returns the key used for the cache map.

Like this: const memoizedFunction = _.memoize(nonMemoizedFunction, (anObject, aBool) => anObject.id + aBool.toString() );

Memoizee's resolver functionality, however, is a lot less clear.

To me, it's not apparent from the documentation how to craft a resolver function and it seems that memoizee focuses on argument type as opposed to allowing for arbitrary key construction.

Is it possible to memoize based on a given object's property instead of attempting to serialize the entire object? What about combining that with a second parameter?

For example, given foo(anObject, aBool), I'd like my memoization key to be anObject.id + bool.

Given how important resolvers are to a memoization library functioning as intended, I think the documentation would really benefit from being beefed up with useful examples as opposed to trivial ones.

medikoo commented 8 years ago

@seanohollaren Firstly, sorry for replying that late, Github notifications failed for me, and it's today I noticed that issue.

resolvers are purely about normalization of arguments, before they're provided into caching mechanism and underlying function. This unifies handling of some cases, e.g. if at some argument you expect a number, the default caching mechanism will treat '2' and 2 differently (as by language they're different values), however if you indicate a Number resolver for that argument, then it's not the case, as '2' will be turned to 2 before it reaches caching handling.

Now concerning normalization of cache id, there's not documented option normalizer (sorry for that, I'll try to address that shortly, and keep this issue open until that's the case).

Through that option you provide a function that's meant to resolve cache id (string) for given call. So in case you're describing it can be:

var memoizedFunction = memoizee(function (anObject, aBool) {
  ...
}, { normalizer: function (args) {
  var anObject = args[0], aBool = args[1];
  return anObject.id + aBool.toString();
});
medikoo commented 8 years ago

Fixed by https://github.com/medikoo/memoizee/commit/75422fd9dc44144d046b53ea8b395d6c5f325c9d

seanohollaren commented 8 years ago

Looks great, thanks!