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
30 changes: 27 additions & 3 deletions code_to_optimize_js_esm/fibonacci.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _fibCache = new Map([[0, 0], [1, 1]]);

/**
* Fibonacci implementations - ES Module
* Intentionally inefficient for optimization testing.
Expand All @@ -10,10 +12,32 @@
* @returns {number} The nth Fibonacci number
*/
export function fibonacci(n) {
if (n <= 1) {
return n;
if (n <= 1) return n;

if (Number.isInteger(n)) {
const cached = _fibCache.get(n);
if (cached !== undefined) return cached;

let a = 0, b = 1;
for (let i = 2; i <= n; i++) {
const c = a + b;
a = b;
b = c;
}

_fibCache.set(n, b);
return b;
}
return fibonacci(n - 1) + fibonacci(n - 2);

function memoFib(x) {
if (x <= 1) return x;
if (_fibCache.has(x)) return _fibCache.get(x);
const val = memoFib(x - 1) + memoFib(x - 2);
_fibCache.set(x, val);
return val;
}

return memoFib(n);
}

/**
Expand Down
Loading