I’ve found examples such as factorial calculation to explain memoization. These are helpful but I’m looking for a deeper understanding.
I’m wondering if someone can describe a real world application of this technique and why they used it instead of recursion or whatever else they felt using memoization might help them optimize.
Memoization is a little more specific than just caching.
Think about searching for an element in the DOM using a selector, like you might with jQuery. Say,
$('.some-selector'). In this context, I’m calling the function$, telling it to find for me all elements that have the CSS selector ‘.some-selector’. Let’s say that the document is large, and I need to call$('.some-selector')many times.You can make the assumption that every call to
$('.some-selector')is going to return the same results, and therefore, doing the actual processing each time it is invoked is wasted effort. Therefore,$could use the argument (‘.some-selector’, in this case) as a key in some lookup table or dictionary. The first time the function is invoked with that argument, it is processed normally, the results are placed in the dictionary using the argument as a key, and the results are returned. Subsequent calls will find that the key has a value in the dictionary representing the results that have already been calculated, so it just returns those previous results. The net effect is that you don’t waste time finding results that you already know.A little crude JavaScript example:
This link goes into more detail, and even includes a generic
memoizeJS function that could be used for any other function.