memoized-fib.js
function makeFib() {
const cache = {};
function fib(n) {
if (n < 2) return n;
if (cache[n] !== undefined) return cache[n];
cache[n] = fib(n - 1) + fib(n - 2);
return cache[n];
}
return fib;
}
const fib = makeFib();
fib(10);
What to watch
The captured cache (badged closure) fills entry by entry in the Data tab; repeat calls short-circuit instead of recursing.
Reading memoized fib (closure)on a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how memoized fib (closure) works.
Run memoized fib (closure) yourself
Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.
Open NeonFlow