Skip to content

Instantly share code, notes, and snippets.

@OdinsHat
Created May 18, 2014 08:50
Show Gist options
  • Save OdinsHat/4aa271992d569431a073 to your computer and use it in GitHub Desktop.
Save OdinsHat/4aa271992d569431a073 to your computer and use it in GitHub Desktop.
Javascript Memoization Example from Javascript the Good Parts
var memoizer = function (memo, formula) {
var recur = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n);
memo[n] = result;
}
return result;
};
return recur;
};
var fibonacci = memoizer([0, 1], function (recur, n) {
return recur(n − 1) + recur(n − 2);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment