Last active
February 6, 2020 06:10
-
-
Save lukeggchapman/b220da63afaf48b7e9cc6f8f13edfad0 to your computer and use it in GitHub Desktop.
Simple memoize method decorator in TS
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
function memoize(_target: object, _key: string, descriptor: PropertyDescriptor) { | |
const cache = {}; | |
const original = descriptor.value; | |
descriptor.value = async function(...args: any[]) { | |
const argsStr = JSON.stringify(args); | |
cache[argsStr] = cache[argsStr] || original.apply(this, args); | |
return cache[argsStr]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment