Last active
December 14, 2015 16:49
-
-
Save nerdpruitt/5118017 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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