Created
February 10, 2021 21:53
-
-
Save keif/3d6073bae1f7a20b0ae44bdd509b6971 to your computer and use it in GitHub Desktop.
Multiple Argument Memoization 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
const constructPropertyFromArgs = function (fn, args) { | |
return [].concat(fn.name, args).join('|'); | |
} | |
const memoize = function (fn) { | |
const cache = {} | |
return function(...args) { | |
const propCheck = constructPropertyFromArgs(fn, args); | |
if (!cache[propCheck]) { | |
cache[propCheck] = fn(...args); | |
} | |
return cache[propCheck]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment