Does try/catch Slow Hot Loops in Node 22?
ملخص
Pixel shares a Node 22 benchmark testing whether try/catch slows a hot loop. Across 50 million iterations, the wrapped loop took 502.9 ms versus 524.6 ms without try/catch, while actually throwing and catching cost about 4.06 microseconds per iteration—showing why modern performance advice should rely on measurement.
DEV Community
I Measured Whether try/catch Still Slows a Hot Loop
Old advice says wrap less code in try/catch because it's slow, so I measured it in Node 22 A 50-million-iteration loop ran in 524.6ms plain and 502.9ms wrapped in try/catch, no real gap Entering a try/catch that never throws cost about 0.002 microseconds per iteration Actually throwing and catching cost about 4.06 microseconds per iteration, roughly 2,000 times more Wrapping a hot loop in try/catch used to be something people avoided on purpose. I ran 50 million iterations plain and 50 million wrapped in try/catch on Node 22, and the wrapped version came back at 502.9 milliseconds against 524.6 for the plain loop, which isn't a penalty at all. Where the "try/catch is slow" advice came from The warning isn't made up. Older JavaScript engines genuinely struggled to optimize code inside a try block, and advice from that era said to keep the expensive work in a separate function and call it from inside a thin try/catch, so the optimizer could still do its job on the part that mattered. That advice spread everywhere: style guides, interview answers, code review comments passed down without anyone checking if it still applied. It's one of those rules that keeps getting repeated past the point where it's still true, because repeating a rule is a lot less work than testing it. I wanted a number from today's engine instead of a rule from memory. So I built a straightforward test. A loop running 50 million times, each iteration doing a small amount of real math, Math.sqrt(i) % 7, just enough work that an empty loop body wouldn't get optimized away into nothing. One version plain, one version with the exact same body wrapped in a try/catch that never has anything to catch. I also tested the specific workaround the old advice recommends: keep the math in a separate function, call that function from inside a thin try/catch, so the optimizer only has to deal with a function call inside the try block instead of the real work. If that workaround still mattered, the separated version should beat the inline one by a clear margin. It didn't. The separated-function version averaged 510.3 milliseconds across five runs. The version with the math written directly inside the try block averaged 503.9. That's a 6-millisecond gap on a 50-million-iteration test, well inside the run-to-run noise I was already seeing on identical code. The workaround isn't earning its extra indirection anymore. What I actually measured Both versions ran five times each, timed with process.hrtime.bigint(), which Node's own documentation describes as a monotonic high-resolution clock meant for exactly this kind of measurement. A monotonic clock matters here because it's not tied to the system clock, so nothing like a clock sync in the background can make one run look artificially slow or fast. The plain loop averaged 524.6 milliseconds across five runs: 511.9, 500.7, 587.0, 521.1, and 502.1. The try/catch version averaged 502.9 milliseconds: 530.0, 509.1, 491.7, 494.8, and 489.2. That's not a typo. The wrapped version came back slightly faster on average, though both sets of numbers overlap enough that the honest read is "no measurable difference," not "try/catch makes code faster." Run-to-run noise on shared hardware is bigger than whatever gap exists here. Five runs each isn't a rigorous benchmark suite. It's enough to say the old advice doesn't hold up on this engine, on this test, today. If you want more confidence than that, run it yourself. It's eleven lines of code. Zero milliseconds of penalty for wrapping code that never throws. That's the headline, and it held up every time I reran it. I threw in one more variant for good measure: try/finally with no catch block at all, the pattern people reach for when they want cleanup code to run but aren't handling an error. That one averaged 548.4 milliseconds, a touch higher than the plain loop's 524.6, but still inside the same noisy band every version of this test landed in. None of the three wrapped versions separated ...

Powered by Readybot.io