Experiment 3D2988F: Reduce duration
Base 9fbd4a2 Metric ID bbf2b1a
rad-experiment show 3d2988f6528066d03b41288c1e7de7ebc1928ebf
by vagrant · Mar 31 14:13 2026
Memoized recursion with Map cache — retains recursive structure, removes O(2^n) redundancy and string round-trips
Delta+100.00%
Metricduration (ms)
Criterialower_is_better
LOC+12 -25
-010203040base123456789101112131415EXPERIMENTS
Kept Discarded Running best
Measurements
MetricBaselineCandidateDelta
duration (ms)How long the code takes to run. Less is better.38.240 ms (n=5)0.000 ms (n=5)+100.00%
ops_per_sec (ops/sec)Operations completed per second. More is better.26.150 ops/sec272776.970 ops/sec+1043024.16%
p95 (ms)Measures p95 in ms. Less is better.38.960 ms0.010 ms+99.97%
Diff
~ src/index.ts
@@ -4,36 +4,23 @@
4 * For n = 30, the expected result is 1,346,268. 5 */ 6 export function sumFibonacci(n: number): number { 7- const fibs: number[] = []; 7+ const memo = new Map<number, number>(); 8 9+ let sum = 0; 10 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); 11+ sum += fibonacci(i, memo); 12 } 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); 20- 21- return total; 13+ return sum; 14 } 15 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); 16+function fibonacci(n: number, memo: Map<number, number>): number { 17+ if (n <= 0) return 0; 18+ if (n === 1) return 1; 19 34- if (num <= 0) return 0; 35- if (num === 1) return 1; 20+ const cached = memo.get(n); 21+ if (cached !== undefined) return cached; 22 37- // Naive double recursion — no memoization, no caching 38- return fibonacci(num - 1) + fibonacci(num - 2); 23+ const result = fibonacci(n - 1, memo) + fibonacci(n - 2, memo); 24+ memo.set(n, result); 25+ return result; 26 }
Environment
Base commit9fbd4a2599b8216c739e720cf014c3f890515ec7
Candidate commite9b750c4620819c76d983e68f5e654bbbe954ca3
Buildtrue
Teststrue
Metric IDbbf2b1a
Archaarch64
OSLinux 6.8.0-86-generic
CPU-
Agentclaude-code / claude-opus-4-6
Filessrc/index.ts