Created
November 1, 2019 19:57
-
-
Save fob2257/a1c6dff17e19280315e001606e220206 to your computer and use it in GitHub Desktop.
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 memo = fn => { | |
const cache = {}; | |
return function () { | |
// arguments belongs to our returned anonymous function | |
// fn is the function we wish to apply this arguments | |
const key = JSON.stringify(arguments); | |
if (!!!cache[key]) cache[key] = fn.apply(null, arguments); | |
console.log({ cache }); | |
return cache[key]; | |
}; | |
}; | |
const fibo = memo(function innerFn(n) { | |
if (n < 2) return 1; | |
return fibo(n - 2) + fibo(n - 1); | |
}); | |
fibo(10); | |
// now fibonacci of 10 is "cached" | |
fibo(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment