March 13, 2026

Stack Wars: Iteration Strikes Back

Removing recursion via explicit callstack simulation

From pretty recursion to crash‑proof hacks: fans cheer, critics say “just raise the stack”

TLDR: A developer shows how to convert neat recursive code into a manual, step-by-step version to avoid crashes from deep call chains. Commenters split between praising the practical workaround, touting save-and-resume checkpoints, and skeptics who argue you should just increase the system’s stack size.

A dev dropped a brainy how‑to for turning elegant “recursion” (calling a function inside itself) into a manual, step‑by‑step workaround that avoids those dreaded crash‑and‑burn moments when apps go too deep. Translation: take your code’s natural nesting and simulate it yourself with a DIY call list. It’s less cute, a bit slower, but way safer for long jobs. The community immediately split into camps, and the comments became the main event.

On Team Applause, azdavis loved the nerdy rigor—“benchmarking” and “property testing” (think: speed trials and automated truth checks)—and even dropped a similar post. Meanwhile, LegionMammal978 came in hot with real‑world pain: iteration lets you save progress to disk and pick up later, aka checkpoints. Big jobs take days; nobody wants to lose them because you got too fancy with recursion.

Cue the drama: bawolff asked if this is just for people who can’t tweak their system’s stack size (the memory limit for nested calls), then dropped the mic with “just increase the stack.” The thread turned into Team Rewrite vs Team Turn‑The‑Dial, with memes about “recursion fans clutching pearls” and jokes that simulating your own stack is like bringing your own ladder to a climb. The vibe? Practical hacks vs pure elegance—and everyone brought receipts

Key Points

  • The article presents a method to convert recursive code into iterative code by simulating the call stack explicitly.
  • It uses mutable records to represent stack frames, trading clarity and some performance for stack safety.
  • TypeScript is used for examples; the approach applies to languages with mutability and parametric polymorphism, with type narrowing being helpful.
  • A recursive sum over a linked list can overflow the stack in node.js on very long inputs (~10k), illustrating the problem.
  • An iterative list-sum example shows how left-to-right accumulation and the list’s linear structure avoid deferred work and stack overflow.

Hottest takes

"Nice post, I like the benchmarking and property testing." — azdavis
"the ability to periodically save the state of the computation to disk, and resume it later." — LegionMammal978
"This seems like a lot of work when you could just increase the stack size instead." — bawolff
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.