-
Notifications
You must be signed in to change notification settings - Fork 2
define equivalence on argument lists #5
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I think that it should define a |
My inclination is to let the developer supply any Map-like object as a cache, and to supply tuple keys to The tuple keys would have the structure |
A strange option. |
@zloirock: |
@js-choi anyway - I think it's good for custom cach strategy, but I think that the user should be sure that with the default cach strategy the method will not be called again with the same arguments and for arguments that GCed and no longer available the result will not pollute memore. |
Yes, the question of what caching policy we should use by default (unbounded memory versus garbage-collectable weak references if possible) is complex, but it probably should go more in #3. As far as this particular issue goes (what function calls are considered equivalent), I definitely think that |
@js-choi Map keys don't use strict equality comparison, they use SameValueZero (and replace |
@michaelficarra: Ah, yes, that is true…I wonder if this disqualifies Map-like caches. Or would we be fine with using Map-likes and caching |
@js-choi If only Maps and Sets had used SameValue... |
@michaelficarra: Well, if we allow developers to supply custom user-supplied map-like caches, then the developer can supply a map-like object that treats f.memo(new MapLikeCacheThatDistinguishesNegativeZero) So the problem would be more about what default behavior we want. And, given that the Committee already decided that ordinary Maps should use SameValueZero, it likely will resolve on SameValueZero as the default memoization argument-matching behavior, too. |
I'm not sure, what do you mean? Tuples can't store objects - only primitives, Records and other Tuples. |
@zloirock: Ah, yes, I had forgotten that. Let’s continue in #4 (comment). |
Regardless of how the cache is built, the comparator function only really cares if the arguments for two different calls produce the same result. Or to get a little more in-depth, it cares if the cache key produced by the arguments of two different calls are the same. It may be helpful to split up the idea of creating keys and comparing keys into different functions, say let extraParam = 2;
const memoFn = Function.memoize(fn, {
hash: function (...args) {
return [
this, // <- at call time, 'this' is captured as part of the cache key
extraParam, // <- at call time, extraParam is captured as part of the cache key
...args
];
},
compare: (keyA, keyB) =>
keyA.length === keyB.length &&
keyA.every((key, index) => key === keyB[index])
)
}); |
The most important thing for this proposal to figure out is how it determines that a subsequent call is "equivalent" to a previous call so that it can return the stored result. This will involve at least
this
andnew.target
values contribute.and probably more.
The text was updated successfully, but these errors were encountered: