-
Notifications
You must be signed in to change notification settings - Fork 12
Only cache if an object as a parameter is the SAME object (===), not if it has the same content #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You are right. For complex objects to be considered equal they have to refer to the exact same references in memory. There is no performant or reliable way to softly compare complex objects in JavaScript. Consider circular objects that recursively refer back to themselves. That's why there's been a big movement recently to write immutable js. |
The objects in the test above are not mutated objects. |
You should at least document the behavior. Even better provide an interface similar to https://github.com/erikras/lru-memoize |
That's a good idea.
Also a reasonable idea, I will look into that, but:
Isn't this easily worked around with:
If I did implement custom comparators, there would be a performance hit for using them, risk of recursive objects leaking in, risk of bad compare functions being passed in (it's not easy making a good compare function, there are a lot of oddities like NaN !== NaN, etc) - using them should probably only be reserved for when absolutely necessary, it can be avoided in the aforementioned case. |
I added documentation about it here: https://github.com/thinkloop/memoizerific#strict-equality And created this issue: #10 |
New comment relevant to this discussion: #11 (comment) |
Snippet from #11 (comment): Another technique is to wrap the memoized function with another function that does any needed preprocessing. For example, say you wanted to memoize ajax(url, opts) where opts is an object of options. Typically this would be called with an inline object for the options like this:
And now |
Added workaround to readme: https://github.com/thinkloop/memoizerific/blob/master/README.md#wrapper-function |
It looks like that you only have a cache hit if an object in the parameters is
===
across calls.See this test: ChrisCinelli@b7083c1
The text was updated successfully, but these errors were encountered: