Skip to content

Instantly share code, notes, and snippets.

@ducin
Created August 6, 2013 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducin/6162944 to your computer and use it in GitHub Desktop.
Save ducin/6162944 to your computer and use it in GitHub Desktop.
caching function results (single argument function)
function fib(x) {
if (x < 2)
return 1;
return fib(x-1) + fib(x-2);
}
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