medikoo / memoizee

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

Is there a way to dynamically configure the size? #128

Open mfbx9da4 opened 2 years ago

medikoo commented 2 years ago

@mfbx9da4 can you describe the use case?

mfbx9da4 commented 2 years ago

Suppose you have a list of items. Each item has some derived data which you would like to cache so that stays referentially identical to avoid unnecessary re-renders in React. The number of items to cache will depend on the user and will change over time. It's easy to know how big that list is at runtime and therefore trivial to know how big that cache size should be but impossible to have a good default size up front.

medikoo commented 2 years ago

@mfbx9da4 This looks to me as a use case that's best served by weak map based configuration.

So the cache is attached to an item, and if that item is disposed, then derived data you cached for it will also be disposed

mfbx9da4 commented 2 years ago

WeakMap is not available in react native

medikoo commented 2 years ago

WeakMap is not available in react native

What do you mean? WeakMap is a native JavaScript interface and this is a package that requires the JavaScript runtime to be run

mfbx9da4 commented 2 years ago

Although I haven't tested myself I saw this comment "WeakRef is not supported in Hermes or JSC on Android". I believe them!

medikoo commented 2 years ago

@mfbx9da4 memoizee under the hook doesn't use WeakRef but WeakMap, which as I search seems supported by all engines