Skip to content

Instantly share code, notes, and snippets.

@pinkhominid
Last active December 1, 2020 02:55
Show Gist options
  • Save pinkhominid/586b2d863840fb01785aaac6dd88e110 to your computer and use it in GitHub Desktop.
Save pinkhominid/586b2d863840fb01785aaac6dd88e110 to your computer and use it in GitHub Desktop.
General use memoize
// derived from https://www.30secondsofcode.org/blog/s/javascript-memoization
export function memoize(fn) {
new Proxy(fn, {
cache: new Map(),
apply(target, thisArg, argsList) {
let cacheKey = JSON.stringify(argsList);
if (!this.cache.has(cacheKey)) this.cache.set(cacheKey, target.apply(thisArg, argsList));
return this.cache.get(cacheKey);
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment