Created
December 12, 2023 22:53
-
-
Save RodEsp/8a2a39382d26adff9ae871e3acda1b08 to your computer and use it in GitHub Desktop.
JavaScript Memoize Function
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 = (fn) => { | |
const cache = new Map(); | |
return (...args) => { | |
const key = JSON.stringify(args); | |
if (cache.has(key)) return cache.get(key); | |
const result = fn(...args); | |
cache.set(key, result); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basic, general-purpose, memoize function.