Created
July 17, 2022 09:07
-
-
Save ax2mx/6b762f148da1f989aeef6367dcb5dfd4 to your computer and use it in GitHub Desktop.
Simple memoize wrapper
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
| const argKey = x => x.toString() + ':' + typeof(x); | |
| const generateKey = args => args.map(argKey).join('|'); | |
| const memoize = fn => { | |
| const cache = Object.create(null); | |
| return (...args) => { | |
| const key = generateKey(args); | |
| const val = cache[key]; | |
| if (val) return val; | |
| const res = fn(...args); | |
| cache[key] = res; | |
| return res; | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment