I want to use memoize
but I have a concern that the cache will grow indefinitely until sad times occur.
I couldn't find anything via google/stackoverflow searches.
P.S. I am using lodash v4.
Memoize effectively lets you cache the results of a function for the same arguments. Trite Example: function add(a, b) { return a + b; } add(20, 5); add(10, 10); add(20, 5); add(10, 10); add(20, 5);
Each caller of a memoized function gets its own copy of the cached data. You can clear a memoized function's cache with f. clear(). The function to memoize.
In programming, memoization is an optimization technique that makes applications more efficient and hence faster. It does this by storing computation results in cache, and retrieving that same information from the cache the next time it's needed instead of computing it again.
Lodash retains all the memoized data unless you specify a different Cache type.
The default cache is lodash's MapCache:
https://github.com/lodash/lodash/blob/4.14.0/lodash.js#L1968
The memoized values are stored in different data structures depending on whether the key is suitable for hashing (and whether or not ES6 Map is available in the environment):
https://github.com/lodash/lodash/blob/4.14.0/lodash.js#L1987
https://github.com/lodash/lodash/blob/4.14.0/lodash.js#L5561
If you look at the "set" methods of all of these data structures, you'll notice there is no provision for anything like LRU etc:
Hash#set: https://github.com/lodash/lodash/blob/4.14.0/lodash.js#L1832
ListCache#set: https://github.com/lodash/lodash/blob/4.14.0/lodash.js#L1940
Map#set: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set
Short answer is no.
When you use a memoize
function you accept a contract that:
So only the implementation that keeps data forever can comply with the requirements.
What is very often confused - is a "memory leak" thing with just "inefficient" use of memory.
In this case - if it is a problem for you, then it is your responsibility to re-create a memoized function when it's good for your algorithm. Since it is only you that knows when it is safe to do so, and it cannot be done automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With