Created
May 30, 2016 19:28
-
-
Save adyngom-gist/8eb6697c21df7586a4ae1168037d3e72 to your computer and use it in GitHub Desktop.
Memoization in JS - Fibonacci example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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