Files
react-native/packages/rn-tester/js/examples/Performance/PerformanceApiExample.js
T
Rubén Norte b97f3e779a Move Web performance APIs to private directory (#42769)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42769

Changelog: [internal]

These APIs are not currently enabled in OSS, so moving the modules should be safe and not considered a breaking change.

Reviewed By: NickGerleman

Differential Revision: D53267565

fbshipit-source-id: edd3daa7c5043e44e5fd4b1af074093ed3ef4152
2024-02-14 03:58:10 -08:00

114 lines
3.7 KiB
JavaScript

/**
* 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.
*
* @format
* @flow strict-local
* @oncall react_native
*/
'use strict';
import type MemoryInfo from 'react-native/src/private/webapis/performance/MemoryInfo';
import type ReactNativeStartupTiming from 'react-native/src/private/webapis/performance/ReactNativeStartupTiming';
import RNTesterPage from '../../components/RNTesterPage';
import * as React from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
import Performance from 'react-native/src/private/webapis/performance/Performance';
const {useState, useCallback} = React;
const performance = new Performance();
function MemoryExample(): React.Node {
// Memory API testing
const [memoryInfo, setMemoryInfo] = useState<?MemoryInfo>(null);
const onGetMemoryInfo = useCallback(() => {
// performance.memory is not included in bom.js yet.
// Once we release the change in flow this can be removed.
setMemoryInfo(performance.memory);
}, []);
return (
<RNTesterPage noScroll={true} title="performance.memory">
<View style={styles.container}>
<Button onPress={onGetMemoryInfo} title="Click to update memory info" />
<View>
<Text>
{`jsHeapSizeLimit: ${String(memoryInfo?.jsHeapSizeLimit)} bytes`}
</Text>
<Text>
{`totalJSHeapSize: ${String(memoryInfo?.totalJSHeapSize)} bytes`}
</Text>
<Text>
{`usedJSHeapSize: ${String(memoryInfo?.usedJSHeapSize)} bytes`}
</Text>
</View>
</View>
</RNTesterPage>
);
}
function StartupTimingExample(): React.Node {
// React Startup Timing API testing
const [startUpTiming, setStartUpTiming] =
useState<?ReactNativeStartupTiming>(null);
const onGetStartupTiming = useCallback(() => {
// performance.reactNativeStartupTiming is not included in bom.js yet.
// Once we release the change in flow this can be removed.
setStartUpTiming(performance.rnStartupTiming);
}, []);
return (
<RNTesterPage noScroll={true} title="performance.reactNativeStartupTiming">
<View style={styles.container}>
<Button
onPress={onGetStartupTiming}
title="Click to update React startup timing"
/>
<View>
<Text>{`startTime: ${String(startUpTiming?.startTime)} ms`}</Text>
<Text>{`initializeRuntimeStart: ${String(
startUpTiming?.initializeRuntimeStart,
)} ms`}</Text>
<Text>
{`executeJavaScriptBundleEntryPointStart: ${String(
startUpTiming?.executeJavaScriptBundleEntryPointStart,
)} ms`}
</Text>
<Text>{`executeJavaScriptBundleEntryPointEnd: ${String(
startUpTiming?.executeJavaScriptBundleEntryPointEnd,
)} ms`}</Text>
<Text>{`initializeRuntimeEnd: ${String(
startUpTiming?.initializeRuntimeEnd,
)} ms`}</Text>
<Text>{`endTime: ${String(startUpTiming?.endTime)} ms`}</Text>
</View>
</View>
</RNTesterPage>
);
}
const styles = StyleSheet.create({
container: {
padding: 10,
},
});
exports.title = 'Performance API Examples';
exports.category = 'Basic';
exports.description = 'Shows the performance API provided in React Native';
exports.examples = [
{
title: 'performance.memory',
render: function (): React.Element<typeof MemoryExample> {
return <MemoryExample />;
},
},
{
title: 'performance.reactNativeStartupTiming',
render: function (): React.Element<typeof StartupTimingExample> {
return <StartupTimingExample />;
},
},
];