planttheidea/micro-memoize

Feature request: support for WeakMap

Closed this issue · 5 comments

If the arguments are very big, sometimes WeakMap is the only practical solution

Can you provide an example?

First of all, a weakmap doesn't change that size; a cache of 1 item without a weakmap and a cache of 1 item with a weakmap with that value incurs the same space consumption. The only difference is the garbage collection, where you need to specifically clear the cache without a weakmap.

Second, this library is built with practicality in mind; a real world use-case with explanation of how the existing functionality cannot support it and how a weakmap is the only solution would help.

This is not a planned feature at this time because the use of a weakmap prevents some of the capabilities of this library from being possible (LRU, cache size limit, etc.). However, if I can help you solve your use case, happy to try.

Just to be clear, this library does not use stringification, it uses SameValueZero equality of arguments. I know several libraries out there that claim to be fast but use stringification to handle key storage, but that is an architectural decision I disagree with and therefore did not incorporate.

If you are looking for weakmap support with a memoization library, try memoizee. It is not as fast, but it's more flexible for caching options, and it has a weakmap mode.

I would also recommend doing your own performance analysis instead of referring to academic concepts. While true in an absolute sense, the differences may be negligible. Look for the best combination of speed, footprint, and code simplicity. It may be your bespoke weakmap solution, it may be using only one library, or it may be multiple solutions.