DevTools Hub

Search tools

Search for a developer tool

Algorithms

Recursion vs Iteration Visualizer

Watch a recursive call stack and an iterative loop solve the same problem side by side.

Part of the Algorithms Toolkit
n = 8 (max 15)
Speed6/10
Recursive1 call · max depth 1

Call fib(8)

fib(8)
Iterative0 iterations · O(1) space

Start with a = 0, b = 1

a = 0
b = 1

What this shows

Two solutions to the same problem, run side by side from a shared play/pause/scrubber control: a recursive version, shown as its actual call stack pushing and popping frames, and an iterative version, shown as a loop variable updating in place. Both are real traces of real code, not animations built to illustrate a point — the call counts, stack depths, and results all come from actually running the algorithm.

Factorial vs Fibonacci — two very different stories

What memoization changes

Turn on Memoize recursive calls for Fibonacci and every fib(k) gets cached the first time it's computed — the next call with the same k returns instantly instead of re-exploring the whole subtree. fib(15) drops from 1,973 calls to 29, of which 13 are memo hits shown in violet. That turns the exponential recursion into a linear one — still using O(n) stack depth and now also O(n) memo storage, versus the iterative loop's O(1) of either — which is exactly why memoized recursion is usually described as trading time for space rather than beating iteration outright.

What the colors mean

FAQ

Why does the iterative panel finish so much earlier?

Because it has so much less work to do. Both panels advance on the same shared step counter deliberately — the iterative side reaching "Done" and just sitting there while the recursive side is still hundreds of calls deep is the point. Nothing is sped up or slowed down to make them line up.

Why is the maximum n capped?

Naive recursive Fibonacci's call count grows exponentially, so fib(30) would mean well over a million recorded steps — the cap keeps every run fast to trace and the call stack panel readable. Turning on memoization removes the exponential blowup, so it unlocks a higher n.

Where does this connect to the rest of the toolkit?

See Big O Calculator and Sorting Algorithm Visualizer for the same idea — real, counted operations instead of abstract notation — applied to comparing complexity classes directly and to sorting.

Is anything I do here sent anywhere?

No — every trace runs entirely in your browser. Nothing here is ever sent to a server.

Related tools