Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@koenbok
Created September 11, 2021 23:00
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 koenbok/af3fba4dea50acce348da92f6919cb8e to your computer and use it in GitHub Desktop.
Save koenbok/af3fba4dea50acce348da92f6919cb8e to your computer and use it in GitHub Desktop.
export function memoize<R, T extends (...args: any[]) => R>(f: T): T {
const cache = new Map<string, R>();
return ((...args: any[]) => {
const key = JSON.stringify(args);
if (!cache.has(key)) cache.set(key, f(...args));
return cache.get(key);
}) as T;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment