Created
April 3, 2022 14:24
-
-
Save novitckas/a94dcec96d5892a7ea632bd0e8588e18 to your computer and use it in GitHub Desktop.
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
const getMemoizationSum = () => { | |
const cache = new Map(); | |
return (a, b) => { | |
const cacheKey = `${a}+${b}`; | |
const cachedValue = cache.get(cacheKey); | |
if (cachedValue) { | |
console.log('cached') | |
return cachedValue; | |
} | |
console.log('notCached') | |
const sum = a + b; | |
cache.set(cacheKey, sum) | |
return sum | |
} | |
} | |
const sum = getMemoizationSum(); | |
sum(1, 1); | |
sum(1, 1); | |
sum(1, 2); | |
sum(1, 3); | |
sum(1, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment