Skip to content

Let iOS JS timers sleep until they are due - #59317

Open
hryhoriiK97 wants to merge 2 commits into
react:mainfrom
hryhoriiK97:ios-timer-sleep-until-next-timer
Open

hryhoriiK97 wants to merge 2 commits into
react:mainfrom
hryhoriiK97:ios-timer-sleep-until-next-timer

Conversation

@hryhoriiK97

Copy link
Copy Markdown
Contributor

Summary:

On iOS, RCTTiming (React/CoreModules/RCTTiming.mm) fires JS timers from a display link on the JS thread. While a timer is pending and due within kMinimumSleepInterval (1 second), the display link keeps running and wakes the JS thread on every frame until the timer is due. Timers further away use a one-shot NSTimer (the sleep timer) instead.

Current behavior. Any timer under one second keeps the JS thread waking at the display's frame rate. A single setInterval(fn, 1000) is never more than a second away, so it keeps it waking 60 times per second (120 on ProMotion displays with high frame rates enabled) for as long as the interval exists, even when nothing else happens. Countdowns, clocks, polling, debounced inputs and library timers all do this.

With this change. Behind the new sleepUntilNextTimerIOS feature flag (default false), the display link only runs for timers due within the next two frames. A timer further away is waited for with the existing sleep timer, which already handles timers more than a second away, so the JS thread wakes when the timer is due instead of on every frame. Timers due within two frames, requestAnimationFrame included, still run from the display link. Timers further away now fire when they are due instead of at the next frame after it.

Changelog:

[Internal]

Test Plan:

  • RCTTimingTests (new, RNTesterUnitTests, run by yarn test-ios): 8 tests for when the display link runs and when a sleep timer waits, with the flag on and off; that a sleeping timer is called when due and not before; that a nearer timer moves the sleep timer earlier; that a deleted sleeping timer isn't called; and that the display link pauses once the next timer is further than two frames away. They pass on an iPhone simulator, three runs in a row; with the threshold change disabled, the 4 that need it fail.
  • The pinned clang-format passes on the changed files, and yarn featureflags --verify-unchanged passes.
  • Measurement. RNTester built with xcodebuild in Release and run on a physical iPhone 15 Pro (iOS 26.6.1), on an otherwise idle screen. The two arms are the same binary with the flag switched by a launch environment variable, in the order before, after, after, before, 4 minutes per launch. Counted: display link callbacks and sleep timer wakeups on the JS thread, the JS thread's CPU time (thread_info), the app's CPU time and context switches (getrusage).
Idle screen, per second before this PR change
setInterval(() => {}, 1000): JS thread wakeups 60 2 −97%
Same: JS thread CPU time 2.21 ms 0.47 ms −79%
Same: app CPU time 2.76 ms 1.02 ms −63%
Same: context switches 62 10 −84%
A clock that re-renders every second: app CPU time 8.13 ms 6.38 ms −22%
A chain of timers of 0–1000 ms: app CPU time 2.90 ms 1.66 ms −43%
No timers: app CPU time 0.6 ms 0.6 ms —
  • Timer accuracy. In each launch, 300 setTimeouts with random delays of 0–1.5 s, timed in JS with performance.now(): none fired early, out of order or not at all, in both arms. Median lateness 10.1 → 8.8 ms, 95th percentile 16.9 → 16.6 ms. Timers of 50–500 ms now fire about 1 ms after they are due instead of at the next frame (up to 16.7 ms later). A timer due within two frames that starts while the display link is paused waits for it to restart, as it does today when no other timer is pending; in the timer chain above, the 0 ms and 16 ms timers fired 14.2 ms late on average instead of 8.6 ms, because the display link was paused more often.

🤖 Generated with Claude Code

`RCTTiming` keeps the JS thread's display link running while any timer is
due within a second, which wakes the JS thread on every frame until then. A
`setInterval` of up to a second keeps it waking at the frame rate for as long
as it exists.

Behind the new `sleepUntilNextTimerIOS` feature flag (default `false`), the
display link only runs for timers due within the next two frames, and the
existing sleep timer waits for timers further away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 21, 2026
@facebook-github-tools facebook-github-tools Bot added p: Software Mansion Partner: Software Mansion Partner labels Oct 21, 2026
Regenerate the feature flag files with `yarn featureflags`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hryhoriiK97

Copy link
Copy Markdown
Contributor Author

@cipolleschi @javache This lowers the threshold of RCTTiming's sleep timer from 1 second to two frames, behind a flag, so a pending setInterval stops waking the JS thread on every frame. Does this look like a change you'd accept?

@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 21, 2026
@hryhoriiK97

Copy link
Copy Markdown
Contributor Author

@javache @cipolleschi Is lowering the sleep threshold behind a flag something you'd accept? If yes, I'll rebase it on main.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant