/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
import * as Fantom from '@react-native/fantom';
import * as React from 'react';
import {View} from 'react-native';
let root;
let testViews: React.MixedElement;
function createDeepViewHierarchy(depth: number, breadth: number): React.Node {
if (depth === 0) {
return (
);
}
const children = [];
for (let i = 0; i < breadth; i++) {
children.push(
{createDeepViewHierarchy(depth - 1, breadth)}
,
);
}
return {children};
}
function createViewsWithLargeAmountOfPropsAndStyles(count: number): React.Node {
let views: React.Node = null;
for (let i = 0; i < count; i++) {
views = (
{views}
);
}
return views;
}
Fantom.unstable_benchmark
.suite('View')
.test.each(
[100, 1000],
n => `render ${n.toString()} uncollapsable views`,
() => {
Fantom.runTask(() => root.render(testViews));
},
n => ({
beforeAll: () => {
let views: React.Node = null;
for (let i = 0; i < n; i++) {
views = (
{views}
);
}
// $FlowExpectedError[incompatible-type]
testViews = views;
},
beforeEach: () => {
root = Fantom.createRoot();
},
afterEach: () => {
root.destroy();
},
}),
)
.test.each(
[100, 1000],
n => `render ${n.toString()} views with large amount of props and styles`,
() => {
Fantom.runTask(() => root.render(testViews));
},
n => ({
beforeAll: () => {
// $FlowExpectedError[incompatible-type]
testViews = createViewsWithLargeAmountOfPropsAndStyles(n);
},
beforeEach: () => {
root = Fantom.createRoot();
},
afterEach: () => {
root.destroy();
},
}),
)
.test.each(
[
[5, 4],
[7, 3],
[10, 2],
],
([depth, breadth]) =>
`render deep view hierarchy (depth=${depth.toString()}, breadth=${breadth.toString()})`,
() => {
Fantom.runTask(() => root.render(testViews));
},
([depth, breadth]) => ({
beforeAll: () => {
// $FlowExpectedError[incompatible-type]
testViews = createDeepViewHierarchy(depth, breadth);
},
beforeEach: () => {
root = Fantom.createRoot();
},
afterEach: () => {
root.destroy();
},
}),
)
.test.each(
[10, 50, 100],
n => `reorder ${n.toString()} children (move first to last)`,
() => {
Fantom.runTask(() => root.render(testViews));
},
n => {
let original: React.MixedElement;
let reordered: React.MixedElement;
return {
beforeAll: () => {
const children = [];
for (let i = 0; i < n; i++) {
children.push(
,
);
}
original = (
{children}
);
// Move first child to last
const reorderedChildren = [...children.slice(1), children[0]];
reordered = (
{reorderedChildren}
);
},
beforeEach: () => {
root = Fantom.createRoot();
Fantom.runTask(() => root.render(original));
testViews = reordered;
},
afterEach: () => {
root.destroy();
},
};
},
)
.test.each(
[10, 50, 100],
n => `reorder ${n.toString()} children (swap first two)`,
() => {
Fantom.runTask(() => root.render(testViews));
},
n => {
let original: React.MixedElement;
let reordered: React.MixedElement;
return {
beforeAll: () => {
const children = [];
for (let i = 0; i < n; i++) {
children.push(
,
);
}
original = (
{children}
);
// Swap first two children — both algorithms handle this equally
const swapped = [children[1], children[0], ...children.slice(2)];
reordered = (
{swapped}
);
},
beforeEach: () => {
root = Fantom.createRoot();
Fantom.runTask(() => root.render(original));
testViews = reordered;
},
afterEach: () => {
root.destroy();
},
};
},
);