From 8b209a3b702432bf57ff82bd7285c80c98136d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 26 Nov 2026 06:22:36 -0800 Subject: [PATCH 1/2] Add IntersectionObserver performance benchmarks (#60309) Summary: ## Context Benchmarks should cover how IntersectionObserver is normally configured: one or two thresholds, with ten and one hundred as bounds. ## Changes Keep all public benchmark additions in this first, benchmark-only diff. Cover 1, 2, 10, and 100 thresholds, deep implicit/explicit roots, and target/root ratio reads. Parameterize callback-count assertions for the threshold count. No runtime implementation or API threshold-count limit is changed. ## Baseline benchmark results Optimized Fantom (native + Hermes bytecode), headless Linux, fixed CPU affinity. This benchmark-only baseline includes landed scrollMargin support, but no runtime changes from this stack. Values are median CPU time per complete invocation from the current run; setup/assertions are outside timed work. Scaling cases have 100 measured invocations and cover 1/2 thresholds plus 10/100 bounds. | Benchmark | Baseline CPU | | --- | --- | | `Read intersectionRatio 1000 times` | 5.105 ms | | `Read rnRootIntersectionRatio 1000 times` | 4.962 ms | | `Scroll 100 targets, depth 1, 1 thresholds, implicit root` | 7.238 ms | | `Scroll 100 targets, depth 1, 2 thresholds, implicit root` | 7.513 ms | | `Scroll 100 targets, depth 1, 10 thresholds, implicit root` | 8.560 ms | | `Scroll 100 targets, depth 1, 100 thresholds, implicit root` | 20.442 ms | | `Scroll 100 targets, depth 40, 1 thresholds, implicit root` | 38.032 ms | | `Scroll 100 targets, depth 40, 1 thresholds, explicit root` | 46.293 ms | Scrolling includes mounting and intersection computation, plus callbacks when thresholds are crossed. Ratio rows measure consumer reads, not notification dispatch. Changelog: [Internal] Reviewed By: javache Differential Revision: D134252378 --- .../IntersectionObserver-benchmark-itest.js | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-benchmark-itest.js b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-benchmark-itest.js index a096a6c448cf..5dd0a742d5dc 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-benchmark-itest.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-benchmark-itest.js @@ -12,6 +12,7 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; import type IntersectionObserverType from '../IntersectionObserver'; +import type IntersectionObserverEntryType from '../IntersectionObserverEntry'; import type {Root} from '@react-native/fantom'; import type {HostInstance} from 'react-native'; @@ -425,3 +426,139 @@ Fantom.unstable_benchmark }, }, ); + +let ratioEntry: IntersectionObserverEntryType; +let ratioSum = 0; + +const ratioOptions = { + beforeEach: () => { + Fantom.runTask(() => { + root.render( + , + ); + }); + Fantom.runTask(() => { + observer = new IntersectionObserver(entries => { + ratioEntry = entries[0]; + }); + observer.observe(nullthrows(nodeRef.current)); + }); + ratioSum = 0; + }, + afterEach: () => cleanup(root, observer), +}; + +Fantom.unstable_benchmark + .suite('IntersectionObserver ratio access', { + minTestExecutionTimeMs: 500, + minWarmupIterations: 10, + }) + .test( + 'Read intersectionRatio 1000 times', + () => { + for (let i = 0; i < 1000; i++) { + ratioSum += ratioEntry.intersectionRatio; + } + }, + { + ...ratioOptions, + afterEach: () => { + expect(ratioSum).toBe(1000); + ratioOptions.afterEach(); + }, + }, + ) + .test( + 'Read rnRootIntersectionRatio 1000 times', + () => { + for (let i = 0; i < 1000; i++) { + ratioSum += ratioEntry.rnRootIntersectionRatio; + } + }, + { + ...ratioOptions, + afterEach: () => { + expect(ratioSum).toBeCloseTo(100, 8); + ratioOptions.afterEach(); + }, + }, + ); + +const scalingCases = [ + {depth: 1, thresholdCount: 1, customRoot: false}, + {depth: 1, thresholdCount: 2, customRoot: false}, + {depth: 1, thresholdCount: 10, customRoot: false}, + {depth: 1, thresholdCount: 100, customRoot: false}, + {depth: 40, thresholdCount: 1, customRoot: false}, + {depth: 40, thresholdCount: 1, customRoot: true}, +]; +const scalingTargetRefs = Array.from({length: 100}, () => + createRef(), +); +let scalingNotifications = 0; +let scalingEntries = 0; + +Fantom.unstable_benchmark + .suite('IntersectionObserver scaling', { + minIterations: 100, + minTestExecutionTimeMs: 500, + minWarmupIterations: 5, + }) + .test.each( + scalingCases, + ({depth, thresholdCount, customRoot}) => + `Scroll 100 targets, depth ${depth}, ${thresholdCount} thresholds, ${customRoot ? 'explicit' : 'implicit'} root`, + () => scrollBy1(scrollViewNode, 50), + ({depth, thresholdCount, customRoot}) => ({ + beforeEach: () => { + let content: React.Node = scalingTargetRefs.map((ref, index) => ( + + )); + for (let i = 0; i < depth; i++) { + content = {content}; + } + Fantom.runTask(() => { + root.render( + + {content} + + , + ); + }); + scrollViewNode = nullthrows(scrollViewRef.current); + scalingNotifications = 0; + scalingEntries = 0; + Fantom.runTask(() => { + observer = new IntersectionObserver( + entries => { + scalingNotifications++; + scalingEntries += entries.length; + }, + { + root: customRoot ? scrollViewNode : null, + threshold: + thresholdCount === 1 + ? [0] + : Array.from( + {length: thresholdCount}, + (_, i) => i / (thresholdCount - 1), + ), + }, + ); + for (const ref of scalingTargetRefs) { + observer.observe(nullthrows(ref.current)); + } + }); + }, + afterEach: () => { + const expectedNotifications = 1 + Math.ceil((thresholdCount - 1) / 2); + expect(scalingNotifications).toBe(expectedNotifications); + expect(scalingEntries).toBe(100 * expectedNotifications); + cleanup(root, observer); + }, + }), + ); From 1433a132bc89c5d787f3d38c7cd207be0a8b266f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 26 Nov 2026 06:22:36 -0800 Subject: [PATCH 2/2] Freeze IntersectionObserver threshold configuration (#60310) Summary: ## Context IntersectionObserver exposes normalized threshold arrays as read-only configuration, but a static read-only type does not prevent JavaScript callers from mutating the arrays at runtime. ## Changes Freeze normalized `thresholds` and `rnRootThresholds` arrays at construction without freezing caller-owned inputs. Add public cases for mutation rejection, input independence, and default/scalar/empty configurations. Native threshold lookup and supported threshold counts remain unchanged. Changelog: [General][Fixed] - Keep IntersectionObserver threshold arrays immutable Reviewed By: javache Differential Revision: D134237377 --- .../IntersectionObserver.js | 12 +++-- .../__tests__/IntersectionObserver-itest.js | 46 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js index b5e226710d52..f8659739610b 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js @@ -109,10 +109,14 @@ export default class IntersectionObserver { this._callback = callback; - this._rootThresholds = normalizeRootThreshold(options?.rnRootThreshold); - this._thresholds = normalizeThreshold( - options?.threshold, - this._rootThresholds != null, // only provide default if no rootThreshold + const rootThresholds = normalizeRootThreshold(options?.rnRootThreshold); + this._rootThresholds = + rootThresholds == null ? null : Object.freeze(rootThresholds); + this._thresholds = Object.freeze( + normalizeThreshold( + options?.threshold, + this._rootThresholds != null, // only provide default if no rootThreshold + ), ); this._root = options?.root ?? null; this._rootMargin = normalizeMargin(options?.rootMargin, 'rootMargin'); 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 f82c06fb2b12..4a95cc03fa99 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 @@ -439,6 +439,52 @@ describe('IntersectionObserver', () => { ).toEqual([0]); }); + it('should freeze normalized thresholds without freezing caller arrays', () => { + const threshold = [1, 0, 0.5]; + const rnRootThreshold = [0.5, 1, 0]; + observer = new IntersectionObserver(() => {}, { + threshold, + rnRootThreshold, + }); + + for (const thresholds of [ + observer.thresholds, + nullthrows(observer.rnRootThresholds), + ]) { + expect(Object.isFrozen(thresholds)).toBe(true); + let mutationError: unknown; + try { + Object.defineProperty(thresholds, '0', {value: 1}); + } catch (error: unknown) { + mutationError = error; + } + expect(mutationError).toBeInstanceOf(TypeError); + expect(thresholds).toEqual([0, 0.5, 1]); + } + + expect(Object.isFrozen(threshold)).toBe(false); + expect(Object.isFrozen(rnRootThreshold)).toBe(false); + threshold.reverse(); + rnRootThreshold[0] = 0; + expect(observer.thresholds).toEqual([0, 0.5, 1]); + expect(observer.rnRootThresholds).toEqual([0, 0.5, 1]); + }); + + it('should freeze default, scalar, and empty normalized threshold arrays', () => { + const observers = [ + new IntersectionObserver(() => {}), + new IntersectionObserver(() => {}, {threshold: []}), + new IntersectionObserver(() => {}, {threshold: 0.5}), + new IntersectionObserver(() => {}, {rnRootThreshold: 0.5}), + ]; + for (const testObserver of observers) { + expect(Object.isFrozen(testObserver.thresholds)).toBe(true); + if (testObserver.rnRootThresholds != null) { + expect(Object.isFrozen(testObserver.rnRootThresholds)).toBe(true); + } + } + }); + it('should not throw if rnRootThreshold is null or undefined', () => { expect(() => { // $FlowExpectedError[incompatible-type]