Returns the memoized (cached) function.
Map
object.function
keyword must be used in order to allow the memoized function to have its this
context changed if necessary.cache
by setting it as a property on the returned function.const memoize = fn => {
const cache = new Map();
const cached = function (val) {
return cache.has(val)
? cache.get(val)
: cache.set(val, fn.call(this, val)) && cache.get(val);
};
cached.cache = cache;
return cached;
};
// See the `anagrams` snippet.
const anagramsCached = memoize(anagrams);
anagramsCached('javascript'); // takes a long time
anagramsCached('javascript'); // returns virtually instantly since it's cached
console.log(anagramsCached.cache); // The cached anagrams map
JavaScript, Function
Performs left-to-right function composition for asynchronous functions.
JavaScript, Function
Converges a list of branching functions into a single function and returns the result.
JavaScript, Function
Creates a debounced function that returns a promise.