Created
September 11, 2021 23:00
-
-
Save koenbok/af3fba4dea50acce348da92f6919cb8e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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