Last active
February 8, 2024 14:45
-
-
Save gitdagray/fc87981b00386024b0a4a6de940f0a94 to your computer and use it in GitHub Desktop.
A memoize decorator function that is capable of handling multiple parameters
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
// A memoize decorator function | |
// that is capable of handling multiple parameters | |
export const memoize = (fn) => { | |
const cache = {}; | |
return (...args) => { | |
if (JSON.stringify(args) in cache) { | |
// if you want to verify result comes from cache | |
console.log(cache); | |
return cache[JSON.stringify(args)]; | |
} | |
const result = fn(...args); | |
cache[JSON.stringify(args)] = result; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment