-
-
Save mindplay-dk/511c81fccf31ec8a80e1998e97f2efd1 to your computer and use it in GitHub Desktop.
Memoization function (JavaScript)
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
function memoized(fn) { | |
let oldArgs = null; | |
let cache = null; | |
return (...newArgs) => { | |
if (oldArgs === null || newArgs.some((arg, index) => arg !== oldArgs[index])) { | |
cache = fn(...newArgs); | |
oldArgs = newArgs; | |
} | |
return cache; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment