递归在欺骗你
Recursion is lying to you

原始链接: https://blog.gaborkoos.com/posts/2026-05-09-Your-Recursion-Is-Lying-to-You/

递归对于开发者而言是一种优雅且直观的工具,但它隐藏着一种操作风险:栈溢出。无论函数逻辑多么严谨或是否符合“尾递归”形式,JavaScript 运行时环境并未统一实现尾调用优化(TCO)。因此,在某个环境中看起来安全的函数,在另一个环境中可能会导致崩溃。 核心问题在于代码结构(递归模式)与运行时行为(栈管理)之间的错位。即使从理论上讲 TCO 是可行的,大多数现代引擎也不保证支持,这意味着栈空间的增长始终是一种物理限制。此外,开发者必须区分与栈相关的崩溃和算法效率问题,例如简单递归实现斐波那契数列时出现的指数级时间复杂度。 为了构建可靠、可用于生产环境的代码,应避免依赖隐式的运行时优化。建议采取以下措施: * **限制递归**:仅将其用于深度较浅且有界限的场景。 * **使用迭代**:对于深度可能会增加的用户驱动或数据驱动型输入,应使用迭代。 * **使用蹦床函数(Trampolines)**:如果必须保留递归结构,同时又想避免栈溢出风险,可以使用蹦床函数。 归根结底,应将递归视为一种提升可读性的手段,而非性能保证。当输入规模不可预测时,请选择显式的迭代模式,以确保在所有 JavaScript 环境中的可移植性和稳定性。

```Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 递归在欺骗你 (gaborkoos.com) 5 分,theanonymousone 发布于 44 分钟前 | 隐藏 | 过往 | 收藏 | 4 条评论 帮助 RajT88 21 分钟前 [–] 这是计算机科学入门(CS 101)的内容吧?递归虽然更容易编写,但相比迭代,它的性能更差且风险更高。 回复 valleyer 11 分钟前 | 父评论 | 下一条 [–] JavaScript 居然不支持尾调用优化(TCO),这真不是 CS 101 的问题。这让我很惊讶。 回复 sras-me 10 分钟前 | 父评论 | 上一条 | 下一条 [–] > 递归更容易编写 而且更容易阅读。 回复 veqq 7 分钟前 | 父评论 | 上一条 [–] 风险高? 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索: ```
相关文章

原文

Featured in Node Weekly #624 and Javascript Weekly - 2026-06-02*

Recursion is one of those ideas developers learn early and trust for years. If the recursive step is simple and the base case is correct, the code feels clean and safe.

It is elegant for a reason: many problems are naturally recursive, and the code often mirrors how we explain the logic out loud. For tree walks, nested structures, and divide-and-conquer patterns, recursion can be easier to read than explicit loops.

The catch is physical limits. Even with a correct base case and sound logic, each recursive call still consumes stack space. At some depth, you crash with stack overflow.

If you read Your Debounce Is Lying to You and Your Throttling Is Lying to You, this is the recursion version of the same pattern: elegant abstraction, hidden operational edge. Even dependency management can lie to you, as explored in Your Package Manager Is Lying to You. For a different category of silent failure, Your JS Date Is Lying to You covers the parsing, mutation, and timezone traps built into the JavaScript Date API.

You can run everything below directly in a browser console. Let's start simple: a recursive sum of all integers from 1 to n.

function sum(n) {
  if (n === 0) return 0;
  return n + sum(n - 1);
}

sum(10); 

Now push a big input:

sum(100000); 

What just happened? The function is logically correct, but each call to sum stays on the stack until the one below it returns. At depth 100,000 the runtime runs out of stack space and throws. It has nothing to do with the result being wrong, it is purely a physical limit on how many nested frames the runtime can hold at once.

The usual next step is tail call optimization. The idea is simple: make the recursive call the last thing the function does, so the runtime can reuse the same frame instead of pushing a new one.

Note that sum is not tail-recursive, even though the recursive call appears on the last line. After sum(n - 1) returns, there is still pending work: the result must be added to n. A call is only in tail position when its return value is forwarded immediately, with no pending computation afterward.

The tail-recursive version moves that pending state into an accumulator:

function sumTR(n, acc = 0) {
  if (n === 0) return acc;
  return sumTR(n - 1, acc + n);
}

sumTR(10); 

Here sumTR(...) is the very last thing that happens — no pending +, no pending anything. The running total lives in acc, not in waiting stack frames. In theory, a runtime that implements TCO can execute this in constant stack space regardless of depth.

Now repeat the same stress input:

sumTR(100000); 

Even with correct tail-recursive structure, many JavaScript runtimes still allocate a new stack frame per call and throw at large depth. This surprises developers who expect TCO to be a universal guarantee. ECMAScript 2015 formally specified proper tail calls in strict mode, but most engines never adopted the feature consistently. Some shipped it and then walked it back due to performance regressions. Others never implemented it at all. The result is that you cannot assume tail recursion is stack-safe in production JavaScript, even if the code is correctly structured for TCO.

Fibonacci is the go-to recursion textbook example and it does run into stack limits too, but it carries a second problem that makes it even worse: exponential time complexity.

function fib(n) {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}

Each call branches into two more calls, so the total number of calls grows as O(2ⁿ). fib(30) already makes over a million calls; fib(50) is in the tens of billions. In a browser this freezes the tab long before any stack limit is reached, which makes the failure mode look identical to a stack overflow but have a completely different root cause.

The tail-recursive version of Fibonacci:

function fibTR(n, a = 0, b = 1) {
  if (n === 0) return a;
  if (n === 1) return b;
  return fibTR(n - 1, b, a + b);
}

This version runs in linear time, but it still risks stack overflow at large n due to the same TCO uncertainty. The exponential version is a red herring for this discussion because it fails for a completely different reason: stack overflow and exponential blowup are two separate problems. They look the same from the outside (the page hangs or crashes) but require completely different fixes.

At the time of writing (May 2026), proper tail-call optimization support is not something you can count on across JavaScript runtimes.

Runtime Engine Proper Tail Calls You Can Rely On? Practical Take
Chrome V8 No Do not expect stack-safe tail recursion.
Node.js V8 No Tail-recursive code can still overflow.
Deno V8 No Same operational expectation as Node/Chrome.
Firefox SpiderMonkey No Do not treat tail recursion as a safety guarantee.
Safari JavaScriptCore Inconsistent — JSC has shipped and walked back TCO across versions Do not rely on it; behavior has varied enough across releases that it is not a stable guarantee.
Bun JavaScriptCore-based Engine-dependent, not a cross-runtime guarantee Verify on exact version; do not assume universal behavior.

The key point is portability. Tail recursion is a property of function structure, while stack reuse is a property of runtime implementation. Even if one engine behaves better in one version, production JavaScript usually spans multiple targets, and correctness should not depend on optimizer-specific behavior. A function can be perfectly tail-recursive in shape and still consume stack per call in the environments your users actually run.

Every recursive function can be rewritten iteratively, and that is usually the safest choice in production when input depth can grow. Iteration does not rely on runtime optimizations for stack safety, because it does not consume stack frames per step. This does not mean giving up the recursive mental model. You can still write code that is conceptually recursive but uses an explicit stack or a trampoline to manage control flow without hitting physical limits.

function sumIter(n) {
  let acc = 0;
  for (let i = n; i > 0; i--) acc += i;
  return acc;
}

sumIter(1000000); 

If you want to keep the recursive structure for readability but need to avoid stack growth, you can use a trampoline: a loop that repeatedly calls a function that returns either a final result or another function to call.

function trampoline(fn) {
  let result = fn;
  while (typeof result === 'function') {
    result = result();
  }
  return result;
}

function sumTrampoline(n, acc = 0) {
  if (n === 0) return acc;
  return () => sumTrampoline(n - 1, acc + n);
}

trampoline(() => sumTrampoline(100000)); 

Trampolines trade stack safety for additional function allocations and dispatch overhead, so they are most useful when preserving recursive structure matters more than raw performance.

This approach scales in a way that does not depend on runtime tail-call behavior, which is exactly what you want when input depth can grow. If recursive structure improves readability for a particular problem, these techniques let you keep that mental model with explicit tradeoffs instead of implicit runtime assumptions.

A useful rule of thumb is to keep recursion for small, bounded depths that you control, and switch to iterative control flow as soon as depth is user-driven, data-driven, or operationally uncertain. For hot paths, benchmark both styles, but do not base correctness on assumed TCO.

  • Never assume TCO in JavaScript for production-critical paths.
  • Test with realistic upper bounds, not toy input sizes.
  • Favor iterative implementations when depth can grow.
  • Treat recursion as a readability tool, not a stack-safety guarantee.

Recursion itself is not the enemy, unverified runtime assumptions are. Tail-recursive shape does not automatically make JavaScript stack-safe, and that gap is where many "works on my machine" surprises come from in production.

Use recursion where it improves clarity and depth is genuinely bounded. When depth can grow or input is outside your control, prefer iterative designs that make stack behavior explicit and portable.

联系我们 contact @ memedata.com