Skip to content

Instantly share code, notes, and snippets.

@adyngom-gist
Created May 30, 2016 19:28
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 adyngom-gist/8eb6697c21df7586a4ae1168037d3e72 to your computer and use it in GitHub Desktop.
Save adyngom-gist/8eb6697c21df7586a4ae1168037d3e72 to your computer and use it in GitHub Desktop.
Memoization in JS - Fibonacci example
var Utils = {
memo: {},
fibonacci: function (n) {
var that = this, val;
if (n in that.memo) {
return that.memo[n];
}
else {
if(n === 0 || n === 1) {
val = n;
}
else {
val = that.fibonacci(n - 1) + that.fibonnacci(n - 2);
}
that.memo[n] = val;
return val;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment