Skip to content

Instantly share code, notes, and snippets.

@nerdpruitt
Last active December 14, 2015 16:49
Show Gist options
  • Save nerdpruitt/5118122 to your computer and use it in GitHub Desktop.
Save nerdpruitt/5118122 to your computer and use it in GitHub Desktop.
// A tracing version:
Function.prototype.cached = function(){
var self = this, cache = {};
return function(arg){
if(arg in cache) {
console.log('Cache hit for '+arg);
return cache[arg];
} else {
console.log('Cache miss for '+arg);
return cache[arg] = self(arg);
}
}
}
Math.sin = Math.sin.cached();
Math.sin(1); // Cache miss for 1
Math.sin(1); // Cache hit for 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment