Last active
December 14, 2015 16:49
-
-
Save nerdpruitt/5118122 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
// 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