Search code examples
javascriptcachingoptimizationdesign-patternsmemoization

What is Memoization exactly?


Could memoization be considered as a design pattern or is simply a method for caching?

https://addyosmani.com/blog/faster-javascript-memoization/


Solution

  • In practical sense, design patterns normally define how more than one relatively complex class/object interact. Even the Singleton [anti]pattern is describing not only the way the object is being created but how it is being consumed by other objects...

    Memoization on contrary, is more of a coding technique. While it often uses some kind of a map/dictionary object underneath, that object can be a native, language-specific object, rather than a custom domain-specific or specialized/optimized hand-crafted object that is aware of the context. So, there's not much of design implementation detail.

    So, the key definitive difference IMO is the level of abstraction involved, which is higher in case of design patterns, and very low in coding techniques such as memoization, function composition, currying, and other. This is a matter of definitions, though.


    Wikipedia:

    ... a software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.

    ...

    Design patterns may be viewed as a structured approach to computer programming intermediate between the levels of a programming paradigm and a concrete algorithm.

    -- https://en.wikipedia.org/wiki/Software_design_pattern

    whereas,

    memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

    -- https://en.wikipedia.org/wiki/Memoization