Last active
December 1, 2020 02:55
-
-
Save pinkhominid/586b2d863840fb01785aaac6dd88e110 to your computer and use it in GitHub Desktop.
General use memoize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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