Conversation
A setTimeout() delay of 0 (or a positive sub-millisecond delay, which is truncated to 0 by insert()) is now scheduled as soon as possible instead of being clamped to 1 ms, matching browser behavior. Negative delays, NaN, and values above TIMEOUT_MAX are still clamped to 1 ms, and setInterval() keeps clamping delays below 1 ms to 1 ms to avoid firing as fast as the event loop allows. Refs: nodejs#46596 Assisted-by: pi Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66155 +/- ##
==========================================
- Coverage 90.28% 90.27% -0.02%
==========================================
Files 790 790
Lines 271642 271982 +340
Branches 51846 51927 +81
==========================================
+ Hits 245260 245537 +277
- Misses 16889 16949 +60
- Partials 9493 9496 +3
🚀 New features to boost your workflow:
|
Member
Does this mean at least one event loop turn or within an event loop turn? |
jasnell
reviewed
Sep 20, 2026
jasnell
left a comment
Member
There was a problem hiding this comment.
LGTM once CI is green and the "as soon as possible" is clarified.
jasnell
approved these changes
Sep 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
setTimeout()now accepts a delay of0(and positive sub-millisecond delays, which are truncated to0byinsert()), scheduling the callback as soon as possible instead of silently clamping it to1 ms. This matches browser (Chrome) behavior and resolves the ordering surprise described in #46596.Negative delays,
NaN, and values aboveTIMEOUT_MAXare still clamped to1 mswith the existing warnings.setInterval()continues to clamp delays below1 msto1 msso it does not fire as fast as the event loop allows.Behavior change
Before:
After (matches browsers):
Test plan
test/parallel/test-timers-zero-delay-ordering.jsverifiessetTimeout(fn, 0)runs beforesetTimeout(fn, 1)and thattimers/promises.setTimeout(0)resolves.test-timers*/ mock-timers / promisified tests pass, plus a broader cross-subsystem subset (vm, worker, stream, http, net).Refs: #46596