Closed peteruithoven closed 8 years ago
@peteruithoven yes, it's not perfectly documented, but it's possible to provide custom normalizer function. It's function that should return an unique id for received arguments, and on that basis internal logic will decide whether given case was memoized or not.
In your case it might work well if done as follows:
var memoized = memoize(getSelectedObjectsTransform, {
normalizer: function (args) {
var state = args[0];
return JSON.stringify(state);
}
});
I'm closing it, as it's not a bug report or feature request
@medikoo Thanks for the idea, but that doesn't really use the benefit of immutable data? I'd like to find a solution without a JSON.stringify()
. I'm afraid the id will get very big, when I have a lot of data, I'm not sure what kind of negative impacts that would have.
@peteruithoven in any case unique string id needs to be resolved. If you know the way how to make it short, just do it :) I don't know internal implications of your project, so it's hard for me to help you more on that case.
Alternatively if for same state
structures always same object instances are passed, then you do not need to provide any normalizers, as by default memoization works by checking whether objects are equal, resolved ids are very short in that case.
I'm trying to get create a memoized function that picks "selected" items out of a
...byId
object. I'm hoping to cache the result when the selection didn't change and when the selected objects didn't change. This is important because I'd like to pass the result to other (expensive) regularly memoized functions. Not memoized example:I can implement a crude custom shallow array "memoizer", but is there a way to do this with memoizee? I'd like to benefit from it's other features. Is there for example a way to implement a custom compare function?
An crude implementation of the desired behaivior: