Created
August 6, 2013 09:07
-
-
Save ducin/6162944 to your computer and use it in GitHub Desktop.
caching function results (single argument function)
This file contains 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 fib(x) { | |
if (x < 2) | |
return 1; | |
return fib(x-1) + fib(x-2); | |
} |
This file contains 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
if (!Function.prototype.memoize) { | |
Function.prototype.memoize = function () { | |
var cache = {}; | |
var func = this; | |
return function (x) { | |
if (!(x in cache)) { | |
cache[x] = func.call(this, x); | |
} | |
return cache[x]; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment