Skip to content

Instantly share code, notes, and snippets.

@meagar
Created September 9, 2011 15:53
Show Gist options
  • Save meagar/1206576 to your computer and use it in GitHub Desktop.
Save meagar/1206576 to your computer and use it in GitHub Desktop.
Caching wrapper redux
Function.prototype.cachify = function () {
var cache = {}, self = this;
return function (arg) {
if (arg in cache) {
console.info('cache hit');
return cache[arg];
}
return cache[arg] = self.call(self, arg);
};
};
isPrime = isPrime.cachify();
sin = Math.sin.cachify();
console.info(isPrime(3));
console.info(isPrime(3)); // cache hit
console.info(sin(60));
console.info(sin(60)); // cache hit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment