-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setTimeout imposes minimum 1-millisecond delay #34728
Comments
No strong opinion here, but imo setting @nodejs/timers |
setTimeout 0 and undefined sounds like a programming error to me. |
@devsnek According to https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout, setTimeout of zero is valid, and if the argument is not specified, it is assumed to be zero. |
@fmela that's not true in any implementation I'm aware of. node apparently sets it to 1 and browsers have a minimum between 4-16ms. |
@devsnek https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_args states: "When delay is larger than 2147483647 or less than 1, the delay will be set to 1." So this is intended behavior. I don't understand the justification for imposing a minimum 1-millisecond delay, though. |
See nodejs/node-v0.x-archive#593 and commit 7fc835a - basically, it's because node wanted to mimic browser behavior. Preempting the "why not 4 ms then?" question: because there's no frame rate in node. |
The current behavior is quite intentional - I think there is an issue from before (other than the one Ben linked to) I recall having to align with this behavior in Sinon (I help maintain fake-timers). |
@fmela and thank you for raising this issue and caring. We need more people to care about this, be engaged and contribute in timers and figure out what the correct behavior should be :] I appreciate it.
|
Closing, no follow-up. |
To be clear, if at any point you wish to follow up @fmela we are happy to have a discussion here and figure out how to proceed. Maybe @itayperry (who made a bunch of PRs to sinon) wants to pick up the docs PR part? |
I would love to help in any way possible :) |
@itayperry this needs a docs pr basically |
Is the docs update needed for explaining the current built-in behavior, or should I do the pr only if an actual change was made? |
Thank you @benjamingr. Sorry, I lost track of this issue. My two cents: the considerations of browsers, which are user-facing and must maintain interactive responsiveness, are not concerns that should be shared by node. It's reasonable to impose a minimum delay in node already provides the I feel that node's implementation should trust the developer when they give a timeout of zero. It's not hard to imagine unintended latency on the order of milliseconds being introduced in server-side node applications in which the developer hasn't realized that Thanks for your consideration! |
Chromium now removes the clamping: https://chromestatus.com/feature/4889002157015040
|
Thanks for bringing this up @nullhook. I am going to reopen this as it does seem to become an option to change |
Closing given there's now an actual discussion on this topic in #46596 which was part of the tsc-agenda. I don't believe we need two issues tracking the same matter. |
What steps will reproduce the bug?
Using the following code, the counter reaches around 690 on my machine:
Specifying a delay of 1 instead of 0 produces the same result. Changing "setTimeout(doit, 0)" to "setImmediate(doit)" results in the counter reaching around 580,000.
How often does it reproduce? Is there a required condition?
This occurs any time setTimeout is invoked with a delay argument that is zero.
What is the expected behavior?
setTimeout with a delay of 0 should not impose a 1-millisecond delay.
What do you see instead?
setTimeout imposes a minimum 1-millisecond delay.
The text was updated successfully, but these errors were encountered: