-
-
Save kepta/d29efe26b9468a0f7620c2bc8dde781c 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 weakCache(fn) { | |
var cache = new WeakMap(); // <-- Behold the Weak! | |
return (arg) => { | |
if (cache.has(arg)) { | |
return cache.get(arg); | |
} | |
var computed = fn(arg); | |
cache.set(arg, computed); | |
return computed; | |
} | |
} | |
cachedCalcArea = weakCache(calcArea); | |
cachedCalcArea(USSR); // cache miss | |
cachedCalcArea(USSR); // cache hit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment