Skip to content

Commit c31ebbd

Browse files
rubennortefacebook-github-bot
authored andcommitted
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
1 parent 2fed45c commit c31ebbd

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

‎packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-benchmark-itest.js‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1313

1414
import type IntersectionObserverType from '../IntersectionObserver';
15+
import type IntersectionObserverEntryType from '../IntersectionObserverEntry';
1516
import type {Root} from '@react-native/fantom';
1617
import type {HostInstance} from 'react-native';
1718

@@ -425,3 +426,139 @@ Fantom.unstable_benchmark
425426
},
426427
},
427428
);
429+
430+
let ratioEntry: IntersectionObserverEntryType;
431+
let ratioSum = 0;
432+
433+
const ratioOptions = {
434+
beforeEach: () => {
435+
Fantom.runTask(() => {
436+
root.render(
437+
<View ref={nodeRef} style={{width: 50, height: 20, margin: 10}} />,
438+
);
439+
});
440+
Fantom.runTask(() => {
441+
observer = new IntersectionObserver(entries => {
442+
ratioEntry = entries[0];
443+
});
444+
observer.observe(nullthrows(nodeRef.current));
445+
});
446+
ratioSum = 0;
447+
},
448+
afterEach: () => cleanup(root, observer),
449+
};
450+
451+
Fantom.unstable_benchmark
452+
.suite('IntersectionObserver ratio access', {
453+
minTestExecutionTimeMs: 500,
454+
minWarmupIterations: 10,
455+
})
456+
.test(
457+
'Read intersectionRatio 1000 times',
458+
() => {
459+
for (let i = 0; i < 1000; i++) {
460+
ratioSum += ratioEntry.intersectionRatio;
461+
}
462+
},
463+
{
464+
...ratioOptions,
465+
afterEach: () => {
466+
expect(ratioSum).toBe(1000);
467+
ratioOptions.afterEach();
468+
},
469+
},
470+
)
471+
.test(
472+
'Read rnRootIntersectionRatio 1000 times',
473+
() => {
474+
for (let i = 0; i < 1000; i++) {
475+
ratioSum += ratioEntry.rnRootIntersectionRatio;
476+
}
477+
},
478+
{
479+
...ratioOptions,
480+
afterEach: () => {
481+
expect(ratioSum).toBeCloseTo(100, 8);
482+
ratioOptions.afterEach();
483+
},
484+
},
485+
);
486+
487+
const scalingCases = [
488+
{depth: 1, thresholdCount: 1, customRoot: false},
489+
{depth: 1, thresholdCount: 2, customRoot: false},
490+
{depth: 1, thresholdCount: 10, customRoot: false},
491+
{depth: 1, thresholdCount: 100, customRoot: false},
492+
{depth: 40, thresholdCount: 1, customRoot: false},
493+
{depth: 40, thresholdCount: 1, customRoot: true},
494+
];
495+
const scalingTargetRefs = Array.from({length: 100}, () =>
496+
createRef<HostInstance>(),
497+
);
498+
let scalingNotifications = 0;
499+
let scalingEntries = 0;
500+
501+
Fantom.unstable_benchmark
502+
.suite('IntersectionObserver scaling', {
503+
minIterations: 100,
504+
minTestExecutionTimeMs: 500,
505+
minWarmupIterations: 5,
506+
})
507+
.test.each(
508+
scalingCases,
509+
({depth, thresholdCount, customRoot}) =>
510+
`Scroll 100 targets, depth ${depth}, ${thresholdCount} thresholds, ${customRoot ? 'explicit' : 'implicit'} root`,
511+
() => scrollBy1(scrollViewNode, 50),
512+
({depth, thresholdCount, customRoot}) => ({
513+
beforeEach: () => {
514+
let content: React.Node = scalingTargetRefs.map((ref, index) => (
515+
<View
516+
key={index}
517+
ref={ref}
518+
style={{position: 'absolute', width: 10, height: 100}}
519+
/>
520+
));
521+
for (let i = 0; i < depth; i++) {
522+
content = <View style={{width: 100, height: 100}}>{content}</View>;
523+
}
524+
Fantom.runTask(() => {
525+
root.render(
526+
<ScrollView ref={scrollViewRef}>
527+
{content}
528+
<View style={{width: 100, height: 1000}} />
529+
</ScrollView>,
530+
);
531+
});
532+
scrollViewNode = nullthrows(scrollViewRef.current);
533+
scalingNotifications = 0;
534+
scalingEntries = 0;
535+
Fantom.runTask(() => {
536+
observer = new IntersectionObserver(
537+
entries => {
538+
scalingNotifications++;
539+
scalingEntries += entries.length;
540+
},
541+
{
542+
root: customRoot ? scrollViewNode : null,
543+
threshold:
544+
thresholdCount === 1
545+
? [0]
546+
: Array.from(
547+
{length: thresholdCount},
548+
(_, i) => i / (thresholdCount - 1),
549+
),
550+
},
551+
);
552+
for (const ref of scalingTargetRefs) {
553+
observer.observe(nullthrows(ref.current));
554+
}
555+
});
556+
},
557+
afterEach: () => {
558+
const expectedNotifications = 1 + Math.ceil((thresholdCount - 1) / 2);
559+
expect(scalingNotifications).toBe(expectedNotifications);
560+
expect(scalingEntries).toBe(100 * expectedNotifications);
561+
cleanup(root, observer);
562+
},
563+
}),
564+
);

0 commit comments

Comments
 (0)