Skip to content
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
16 changes: 13 additions & 3 deletions code_to_optimize_js/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@
* @returns {number} - The nth Fibonacci number
*/
function fibonacci(n) {
if (n <= 1) {
return n;
const cache = new Map();
function fib(x) {
if (x <= 1) {
return x;
}
const cached = cache.get(x);
if (cached !== undefined) {
return cached;
}
const result = fib(x - 1) + fib(x - 2);
cache.set(x, result);
return result;
}
return fibonacci(n - 1) + fibonacci(n - 2);
return fib(n);
}

/**
Expand Down
Loading