Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the default lodash memoize function a danger for memory leaks?

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.

like image 840
ctrlplusb Avatar asked Jul 26 '16 21:07

ctrlplusb


People also ask

What does Lodash memoize do?

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);

How do I clear my memoize cache?

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.

What is memoize Javascript?

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.


2 Answers

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

like image 120
chardy Avatar answered Oct 13 '22 08:10

chardy


Short answer is no.

When you use a memoize function you accept a contract that:

  1. The function will only be invoked once with a given argument
  2. The cache stays there as long as it's necessary to guarantee the #1 (forever)

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.

like image 41
zerkms Avatar answered Oct 13 '22 07:10

zerkms