Skip to content
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

The setInterval function can only be called 66 times per second under Windows. #57191

Open
FishOrBear opened this issue Feb 24, 2025 · 4 comments
Labels
timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. windows Issues and PRs related to the Windows platform.

Comments

@FishOrBear
Copy link

Version

22.9

Platform

win10 22H2

code:

let i = 0;
setInterval(() =>
{
    i++;
}, 10);

setInterval(() =>
{
    console.log(i);
    i = 0;
}, 1000);

Subsystem

No response

What steps will reproduce the bug?

node

How often does it reproduce? Is there a required condition?

.

What is the expected behavior? Why is that the expected behavior?

100/s

What do you see instead?

66/s

Additional information

No response

@juanarbol
Copy link
Member

Hi! I don't think this is an issue.

Is the interval function malfunctioning? If your complaints are about speed; feel free to send a PR so Node.js will achieve your threshold.

@jasnell
Copy link
Member

jasnell commented Feb 24, 2025

This is not a bug and setInterval() is working as expected. There are reasonable limitations in the way timers work in single threaded event loops and this is just highlighting those. Recommend marking this issue as invalid and closing.

@FishOrBear
Copy link
Author

It is much faster on Linux, only slower on Windows

@bnoordhuis
Copy link
Member

The explanations above are not incorrect (node is not a real-time system) but the core issue here is the default 64 Hz granularity of timers on Windows, meaning the operating system groups timers in intervals of 1,000/64 = 15.625 ms. A 10 ms timer is prone to getting rounded up to the next interval.

Note how 64 is very close to the 66 calls/sec you're seeing. For comparison, on my Linux machine it hovers around ~97 calls/sec.

One improvement node could maybe make is call timeBeginPeriod(1) to request better timer granularity (like e.g. Chrome does) but that's a precision-vs-efficiency trade-off. Higher precision means higher CPU usage, ceteris paribus.

@bnoordhuis bnoordhuis added timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. windows Issues and PRs related to the Windows platform. labels Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

4 participants