Skip to content

Instantly share code, notes, and snippets.

@kepta

kepta/block3.js Secret

Created April 11, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kepta/1bfbd06514135dccca1ec7531c95b652 to your computer and use it in GitHub Desktop.
Save kepta/1bfbd06514135dccca1ec7531c95b652 to your computer and use it in GitHub Desktop.
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