Skip to content

refactor: add memoized fibonacci example #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lessons/recursion.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ Let's break this down call-by-call. Since recursive functions will break down in

So what if we call `fibonacci(30)`? The answer is 832040. You guessed it, we add 1 to itelf, 832040 times. What if we call `fibonacci(200)`? Chances are you'll get a stack overflow.

To optimize our recursive solution, we can use *memoization*. This technique stores already computed values, reducing the need to recalculate them and thus improving performance. Here's how a memoized version of the `fibonacci` function looks:

```javascript
function fibonacci(n, memo = {}) {
if (memo[n]) return memo[n];

if (n === 1 || n === 2) return 1;
if (n <= 0) return 0;

memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
return memo[n];
}
```

Not very efficient here, but very elegant code. Here you'd need to trade off having readable code versus a relatively poor performance profile. If your use case says you'll only need to call no more than with n = 10, yeah, this is probably okay. If you need to call it with n = 200, you need to rewrite it to something different.

Here's the iterative solution:
Expand Down