Considerations

Memoization can only be applied to pure functions. 

What is a pure function? A function is called pure when it always returns the same value given the same input. It may seem intuitive for every function to behave that way but in practice, it is not that straightforward. Some functions are not pure due to reasons such as these:

  • A function uses a random number generator and is expected to return random results.
  • A function relies on data from an external source that produces different data at different times.

Because the memoization pattern uses function arguments as the key of the in-memory cache, it will always return the same result for the same key.

Another consideration is that we should be aware of the extra memory overhead due to the use of a cache. It is important to choose the right cache invalidation strategy for the specific use case. Typical cache invalidation strategies include Least Recently Used (LRU), First-In, First-Out (FIFO), and time-based expiration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.4.167