Created
March 13, 2023 15:53
-
-
Save xrip/df0a8a47f0fe5f1f477a6b43b05edd1a to your computer and use it in GitHub Desktop.
Memoize function that saves Typescript types for arguments and return value
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
export const memoize = <ARGS extends unknown[], RETURN>(FUNCTION: (...args: ARGS) => RETURN) => { | |
const memory = new Map<string, RETURN>(); | |
return (...args: ARGS) => memory.get(args.join()) || memory.set(args.join(), FUNCTION(...args)).get(args.join()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment