Skip to content

Speed up disconnecting the final ResizeObserver - #60308

Merged
meta-codesync[bot] merged 1 commit into
react:mainfrom
rubennorte:export-D134238255
Nov 26, 2026
Merged

meta-codesync[bot] merged 1 commit into
react:mainfrom
rubennorte:export-D134238255

Conversation

@rubennorte

Copy link
Copy Markdown
Member

Summary:

Context

Disconnecting a ResizeObserver currently unobserves every target separately. Native unobserve() repeatedly scans the remaining observations, so tearing down a large observation set does quadratic work even when the final native disconnect() will clear the same state.

Changes

  • When the observer is the sole registered observer, use the existing native disconnect to remove all observations together, then clear its JavaScript target map and registration ID.
  • Preserve the per-target unobserve path while any other observer remains registered.
  • Add benchmarks for idle work, unrelated and actual resizes, observer fan-out, final teardown, and sparse trees.
  • Extend the existing integration suite with many-target hide/removal, disconnect/re-observe, and callback reentrancy cases.

Benchmark results

Final-observer disconnect() measured on Linux x86_64 (AMD EPYC 9D64) with optimized native code and Hermes bytecode. Before/after values are the median of the mean CPU latency from three independent runs per variant on the same worker, with 200 measured teardown iterations and 20 warmup iterations per run.

Observed targets Before After Speedup
1 5.24 microseconds 3.69 microseconds 1.4x
100 209.01 microseconds 6.70 microseconds 31.2x
1,000 14.775 milliseconds 38.95 microseconds 379.3x

Registration setup is outside the timed teardown operation; these measure deregistration, not registration, resize checking, notification dispatch, or phone frame time.

Reproduce with yarn fantom ResizeObserver-benchmark-itest --benchmarks from the React Native repository root.

Changelog:
[General][Changed] - Reduce the time spent disconnecting the final ResizeObserver with many targets.

Differential Revision: D134238255

Summary:
## Context

Disconnecting a `ResizeObserver` currently unobserves every target separately. Native `unobserve()` repeatedly scans the remaining observations, so tearing down a large observation set does quadratic work even when the final native `disconnect()` will clear the same state.

## Changes

- When the observer is the sole registered observer, use the existing native disconnect to remove all observations together, then clear its JavaScript target map and registration ID.
- Preserve the per-target unobserve path while any other observer remains registered.
- Add benchmarks for idle work, unrelated and actual resizes, observer fan-out, final teardown, and sparse trees.
- Extend the existing integration suite with many-target hide/removal, disconnect/re-observe, and callback reentrancy cases.

## Benchmark results

Final-observer `disconnect()` measured on Linux x86_64 (AMD EPYC 9D64) with optimized native code and Hermes bytecode. Before/after values are the median of the mean CPU latency from three independent runs per variant on the same worker, with 200 measured teardown iterations and 20 warmup iterations per run.

| Observed targets | Before | After | Speedup |
| --- | --- | --- | --- |
| 1 | 5.24 microseconds | 3.69 microseconds | 1.4x |
| 100 | 209.01 microseconds | 6.70 microseconds | 31.2x |
| 1,000 | 14.775 milliseconds | 38.95 microseconds | 379.3x |

Registration setup is outside the timed teardown operation; these measure deregistration, not registration, resize checking, notification dispatch, or phone frame time.

Reproduce with `yarn fantom ResizeObserver-benchmark-itest --benchmarks` from the React Native repository root.

Changelog:
[General][Changed] - Reduce the time spent disconnecting the final `ResizeObserver` with many targets.

Differential Revision: D134238255
@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 Nov 26, 2026
@meta-codesync

meta-codesync Bot commented Nov 26, 2026

Copy link
Copy Markdown

@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D134238255.

@javache javache left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync
meta-codesync Bot merged commit 9743c09 into react:main Nov 26, 2026
104 of 107 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @rubennorte in 9743c09

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Nov 26, 2026
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. Merged This PR has been merged. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants