DEV Community

Discussion on: A Beginner's Guide: Memoization

Collapse
 
l_giraudel profile image
Loïc Giraudel

As a result, we can see that using the memoize technique in this solution would be a premature optimization - and would negatively impact the performance of our application.

While I agree about preventing premature optimization, on recursive functions like your factorial function memoization can store intermediate results, leading to optimized performance even if you don't call the function with the same parameter.

For instance, factorial(4999) could be faster if factorial memoizes all intermediate values from 4999 to 0. Of course, it means a bigger memory cost since the cache will no longer contain 1 value but 5000. But I think the best use case for memoization is on recursive pure functions.

The drawback is that you can't memoize a function from the outside like you did with memoize-one: the function must be implemented with its own cache.