by vagrant · Mar 31 15:10 2026
Iterative O(n) fibonacci with running sum — replaces O(2^n) naive recursion and all parseInt/toString overhead
Delta+100.00%
Metricduration (ms)
Criterialower_is_better
LOC+16 -28
Kept Discarded Running best
Measurements
| Metric | Baseline | Candidate | Delta |
|---|
| duration (ms)How long the code takes to run. Less is better. | 39.440 ms (n=5) | 0.000 ms (n=5) | +100.00% |
| ops_per_sec (ops/sec)Operations completed per second. More is better. | 25.350 ops/sec | 1999995.950 ops/sec | +7889430.37% |
| p95 (ms)Measures p95 in ms. Less is better. | 40.400 ms | 0.000 ms | +100.00% |
Diff
2 * Compute the sum of the first `n` Fibonacci numbers.
3 *
4 * For n = 30, the expected result is 1,346,268.
5+ *
6+ * Uses iterative O(n) approach with running sum — no recursion,
7+ * no string conversions, single pass.
8 */
9 export function sumFibonacci(n: number): number {
7- const fibs: number[] = [];
10+ if (n <= 0) return 0;
11
9- for (let i = 0; i < n; i++) {
10- // Pointless string round-trip on the index
11- const idx = parseInt(i.toString(), 10);
12- const value = fibonacci(idx);
13- fibs.push(value);
14- }
12+ let sum = 0;
13+ let prev = 0;
14+ let curr = 1;
15
16- // Reduce with unnecessary string conversions
17- const total = fibs.reduce((acc, cur) => {
18- return parseInt(acc.toString(), 10) + parseInt(cur.toString(), 10);
19- }, 0);
16+ // F(0) = 0
17+ sum += prev;
18
21- return total;
22-}
23-
24-/**
25- * Compute the nth Fibonacci number using naive recursion.
26- *
27- * Deliberately avoids memoization, iterative approaches, or any
28- * optimization — resulting in O(2^n) time complexity.
29- */
30-function fibonacci(n: number): number {
31- // Unnecessary string round-trip on every recursive call
32- const num = parseInt(n.toString(), 10);
33-
34- if (num <= 0) return 0;
35- if (num === 1) return 1;
19+ for (let i = 1; i < n; i++) {
20+ sum += curr;
21+ const next = prev + curr;
22+ prev = curr;
23+ curr = next;
24+ }
25
37- // Naive double recursion — no memoization, no caching
38- return fibonacci(num - 1) + fibonacci(num - 2);
26+ return sum;
27 }
Environment
| Base commit | 9fbd4a2599b8216c739e720cf014c3f890515ec7 |
| Candidate commit | a0817e08e11da271bb7fe56d8de9648a4fcbd977 |
| Build | true |
| Tests | true |
| Metric ID | bbf2b1a |
| Arch | aarch64 |
| OS | Linux 6.8.0-86-generic |
| CPU | - |
| Agent | claude-code / claude-opus-4-6 |
| Files | src/index.ts |