Skip to content

Instantly share code, notes, and snippets.

@zspencer
Last active January 29, 2016 18:24
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 zspencer/91e3c06ce855e869c582 to your computer and use it in GitHub Desktop.
Save zspencer/91e3c06ce855e869c582 to your computer and use it in GitHub Desktop.
function findFib(index) {
var values = [0,1];
if(index in values) { return values[index]; }
return findFib(index - 2) + findFib(index - 1);
}
function memoize(fn) {
var cache = {};
return function() {
var args = Array.prototype.slice.call(arguments);
if(!(args in cache)) { cache[args] = fn.apply(this, args); }
return cache[args];
};
}
findFib = memoize(findFib);
console.log(findFib(35));
console.log(findFib(35));
console.log(findFib(35));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment