-
-
Save kepta/1bfbd06514135dccca1ec7531c95b652 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
function cachify(fn) { | |
// create a cache, which only accessible inside the closure | |
var cache = new Map(); | |
return arg => { | |
if (cache.has(arg)) { | |
return cache.get(arg); | |
} | |
var computed = fn(arg); | |
cache.set(arg, computed); | |
return computed; | |
}; | |
} | |
cachedCalcArea = cachify(calcArea); | |
cachedCalcArea(USSR); // goes and computes the area | |
cachedCalcArea(USSR); // already computed, returns the cached area |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment