Repository navigation
perf(indicator): stop reading shared values on every scroll frame - #15
Merged
Merged
Conversation
Apps call onScrollToOffsetPercentage on every scroll frame, and the handle read two shared values there: whether a finger was on the track, and the track's height. On Reanimated 3, reading a shared value from JS runs synchronously on the UI runtime, so each scroll frame blocked the JS thread twice until the UI thread answered. The handle now reads JS copies: the track height kept from onLayout, and a finger-on-track flag set with runOnJS from the pan's onBegin and onFinalize. The worklets still read the layout shared value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
IndicatorCost renders a 2000-row FlashList with FastScrollIndicator and times onScrollToOffsetPercentage on the JS thread: on every onScroll frame, and in a loop of 1000 calls. In the same frames it also times two JS reads of shared values shaped like the ones the handle used to read, so the cost of those reads shows under the same load whatever indicator version runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Reanimated 3 the read takes the UI runtime's lock and runs on the JS thread; it does not hop to the UI thread. It waits while the UI thread runs worklets, which it does on every frame of a scroll. The comment and the backlog item now say that, and the backlog quotes the measured cost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gfrancischini
force-pushed
the
perf/indicator-no-sync-reads
branch
from
November 10, 2026 13:32
2b64f06 to
30cdf80
Compare
gfrancischini
added a commit
that referenced
this pull request
Nov 10, 2026
…r PR #15 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Apps call
onScrollToOffsetPercentageon every scroll frame, and the handle read two shared values there. On Reanimated 3, a JS read of a shared value is synchronous (react-native-reanimated3.16,src/mutables.ts):executeOnUIRuntimeSyncends inWorkletRuntime::executeSync. That function takes the UI runtime's lock and runs the read on the JS thread:So the read does not switch threads, but it waits whenever the UI thread holds that lock to run worklets. While the list scrolls, the UI thread does that on every frame; at the very least it runs this thumb's own springs (
pointStylestarts twowithSpringon eachtranslateYchange). Writing a shared value from JS is asynchronous (runOnUI), so the write of the thumb position stays as it is.disableOnScrollEventis gone: only the JS handle read it. The pan is still built once, sincesetFingerOnTrackis a stableuseCallback.Also in this PR:
example/: anIndicatorCostscreen that times the handle on the device (below).docs/backlog.md: keeps only the dots bar's version of this item. ItsonSectionChangehandle does the same kind of read, but only on viewability changes. The item now quotes the measured cost.Evidence
Time per handle call while the list scrolls. Setup:
expo start --no-dev --minify).IndicatorCostscreen: a 2000-row FlashList with the indicator, one handle call peronScrollframe, 12 flings per session.main)The mean drops from about 3.5 ms to 0.4 ms per frame, and p95 from about 16 ms (almost a whole 60 fps frame) to about 1.6 ms.
In the same frames, the screen also times two JS reads of shared values shaped like the ones the handle read (a flag and a layout object). In every session, with either version, they took p50 0.44–0.79 ms and p95 12–17 ms. That accounts for the gap between the two rows.
With the UI thread idle (1000 handle calls in a row, no scrolling) the two versions cost the same, about 10 µs per call: the read only costs while the UI thread holds the lock.
These are emulator numbers: absolute values on a device will differ, the mechanism does not.
main)Scroll: the handle.Reads: the two probe reads in the same frames.Behavior on Reanimated 3. Categories screen, SDK 52:
Full resolution: thumb-drag-sdk52.mp4.
Unit tests. No new test: under Jest, with Reanimated 2.12, reading
.valueis a plain JS read, so the wait cannot show there. The behavior the change touches is already covered and passes unchanged:yarn test(101 passed, 1 todo),yarn typecheck(against Reanimated 2.12 and 3) andyarn lintpass.Merge Danger
Door: two-way
Internal state only; the props, the handle and the gesture are unchanged. The example screen and the backlog text do not ship.
Blast Radius: indicator thumb
What could go wrong is the order of events around a drag. On a device,
runOnJScalls reach JS in order: the flag is set before the firstscrollToOffsetPercentageof a drag, and cleared after the last one. Before, the flag was a shared value cleared on the UI thread at lift, so the list's last scroll events after a drag could already move the thumb; they still can.🤖 Generated with Claude Code