Skip to content

Instantly share code, notes, and snippets.

@novitckas
Created April 3, 2022 14:24
Show Gist options
  • Save novitckas/a94dcec96d5892a7ea632bd0e8588e18 to your computer and use it in GitHub Desktop.
Save novitckas/a94dcec96d5892a7ea632bd0e8588e18 to your computer and use it in GitHub Desktop.
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