From a06cd757bf580c975e15a5d8fb6250e8e303f78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 26 Nov 2026 10:47:42 -0800 Subject: [PATCH] Avoid temporary rectangles when reading intersection ratios (#60312) Summary: ## Context IntersectionObserverEntry ratio getters construct two temporary rectangles even though they only need stored widths and heights. This adds allocation overhead when consumers process many intersection entries. ## Changes Calculate target and root ratios directly from native rectangle tuples, retaining zero-dimension handling and upper clamping. Add a saved-entry stability integration case after subsequent scrolling and use the shared target/root ratio-read benchmarks; this diff adds no benchmark code. ## Compatibility Ratios derive from stored native geometry; overriding rectangle getters no longer changes the calculated ratios. Public rectangle getters retain their existing behavior. ## Benchmark results Optimized Fantom (native + Hermes bytecode), headless Linux, same CPU affinity. The benchmark-only baseline and ratio optimization use a base containing landed scrollMargin support. Values are median CPU time per complete invocation from the current runs; setup/assertions are outside timed work, and scaling cases have 100 measured invocations. These are workload-specific measurements, not isolated profiler timings or app-wide speedups. | Benchmark | Baseline CPU | Current CPU | CPU reduction | | --- | --- | --- | --- | | `Read intersectionRatio 1000 times` | 5.105 ms | 0.152 ms | 97.0% | | `Read rnRootIntersectionRatio 1000 times` | 4.962 ms | 0.150 ms | 97.0% | These measure callback-consumer ratio reads, not notification dispatch. Changelog: [General][Changed] - Reduce IntersectionObserverEntry ratio-access overhead Reviewed By: javache Differential Revision: D134250349 --- .../IntersectionObserverEntry.js | 33 +++++++++---------- .../__tests__/IntersectionObserver-itest.js | 31 +++++++++++++++++ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverEntry.js b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverEntry.js index 14de06a943ff..16d8276a512e 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverEntry.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverEntry.js @@ -60,16 +60,18 @@ export default class IntersectionObserverEntry { */ // $FlowFixMe[unsafe-getters-setters] get intersectionRatio(): number { - const intersectionRect = this.intersectionRect; - const boundingClientRect = this.boundingClientRect; + const intersectionRect = this._nativeEntry.intersectionRect; + const targetRect = this._nativeEntry.targetRect; + const width = targetRect[2]; + const height = targetRect[3]; - if (boundingClientRect.width === 0 || boundingClientRect.height === 0) { + if (width === 0 || height === 0) { return 0; } - const ratio = - (intersectionRect.width * intersectionRect.height) / - (boundingClientRect.width * boundingClientRect.height); + const intersectionArea = + intersectionRect == null ? 0 : intersectionRect[2] * intersectionRect[3]; + const ratio = intersectionArea / (width * height); // Prevent rounding errors from making this value greater than 1. return Math.min(ratio, 1); @@ -80,23 +82,18 @@ export default class IntersectionObserverEntry { */ // $FlowFixMe[unsafe-getters-setters] get rnRootIntersectionRatio(): number { - const intersectionRect = this.intersectionRect; - + const intersectionRect = this._nativeEntry.intersectionRect; const rootRect = this._nativeEntry.rootRect; - const boundingRootRect = new DOMRectReadOnly( - rootRect[0], - rootRect[1], - rootRect[2], - rootRect[3], - ); + const width = rootRect[2]; + const height = rootRect[3]; - if (boundingRootRect.width === 0 || boundingRootRect.height === 0) { + if (width === 0 || height === 0) { return 0; } - const ratio = - (intersectionRect.width * intersectionRect.height) / - (boundingRootRect.width * boundingRootRect.height); + const intersectionArea = + intersectionRect == null ? 0 : intersectionRect[2] * intersectionRect[3]; + const ratio = intersectionArea / (width * height); // Prevent rounding errors from making this value greater than 1. return Math.min(ratio, 1); diff --git a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js index 4a95cc03fa99..c8e697c23b6e 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js @@ -5164,6 +5164,37 @@ describe('IntersectionObserver', () => { }); }); + it('should keep entry ratios stable after later scrolling updates', () => { + const scrollRef = createRef(); + const targetRef = createRef(); + const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 200}); + const callback = jest.fn(); + Fantom.runTask(() => { + root.render( + + + + , + ); + }); + Fantom.runTask(() => { + observer = new IntersectionObserver(callback, {threshold: [0, 0.5, 1]}); + observer.observe(nullthrows(targetRef.current)); + }); + const initialEntry = callback.mock.lastCall[0][0]; + Fantom.scrollTo(nullthrows(scrollRef.current), {x: 0, y: 50}); + const updatedEntry = callback.mock.lastCall[0][0]; + expect(callback).toHaveBeenCalledTimes(2); + for (let i = 0; i < 2; i++) { + expect(initialEntry.intersectionRatio).toBe(1); + expect(initialEntry.rnRootIntersectionRatio).toBe(0.5); + expect(updatedEntry.intersectionRatio).toBe(0.5); + expect(updatedEntry.rnRootIntersectionRatio).toBe(0.25); + } + Fantom.runTask(() => observer.disconnect()); + root.destroy(); + }); + describe('disconnect', () => { it('should do nothing if no targets are observed (not fail)', () => { const callback = jest.fn();