Skip to content

Instantly share code, notes, and snippets.

@lukeggchapman
Last active February 6, 2020 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeggchapman/b220da63afaf48b7e9cc6f8f13edfad0 to your computer and use it in GitHub Desktop.
Save lukeggchapman/b220da63afaf48b7e9cc6f8f13edfad0 to your computer and use it in GitHub Desktop.
Simple memoize method decorator in TS
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