Skip to content

Instantly share code, notes, and snippets.

@feross
Created February 29, 2012 01:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feross/1936676 to your computer and use it in GitHub Desktop.
Save feross/1936676 to your computer and use it in GitHub Desktop.
memoizer.js
var count = 0;
var memoizer = function(memo, formula) {
var recur = function(n) {
var result = memo[n];
if (typeof result !== "number") {
result = formula(n);
memo[n] = result;
}
return result;
};
return recur;
};
var fibonacci = memoizer([0, 1], function(n) {
return fibonacci(n - 1) + fibonacci(n - 2);
});
console.log(fibonacci(10));
console.log("count: " + count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment