Skip to content

Instantly share code, notes, and snippets.

@nerdpruitt
Last active December 14, 2015 16:49
Show Gist options
  • Save nerdpruitt/5118017 to your computer and use it in GitHub Desktop.
Save nerdpruitt/5118017 to your computer and use it in GitHub Desktop.
Function.prototype.cached = function(){
// reference original function and create cache
var self = this, cache = {};
return function(arg){
// is this argument already in the cache?
if(arg in cache) return cache[arg];
// if not add it
return cache[arg] = self(arg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment