September 2018
Intermediate to advanced
302 pages
7h 17m
English
Caching is a technique of remembering computations. If you are guaranteed that for certain arguments your function will always return the same value, you can safely compute it once and always return that computed value for these specific arguments.
Let's look at the trivial implementation that is usually brought up for teaching purposes:
const memoize = yourFunction => { const cache = {}; return (...args) => { const cacheKey = JSON.stringify(args); if (!cache[cacheKey]) { cache[cacheKey] = yourFunction(...args); } return cache[cacheKey]; }; };
This is a powerful technique and is used in the reselect library.
Read now
Unlock full access