| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and its affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import * as React from 'react'; |
| 11 | |
| 12 | function generateArray(size: number) { |
| 13 | return Array.from({length: size}, () => Math.floor(Math.random() * size)); |
| 14 | } |
| 15 | |
| 16 | const arr = generateArray(50000); |
| 17 | |
| 18 | export default function LargeSubtree(): React.Node { |
| 19 | const [showList, setShowList] = React.useState(false); |
| 20 | const toggleList = () => { |
| 21 | const startTime = performance.now(); |
| 22 | setShowList(!showList); |
| 23 | // requestAnimationFrame should happen after render+commit is done |
| 24 | window.requestAnimationFrame(() => { |
| 25 | const afterRenderTime = performance.now(); |
| 26 | console.log( |
| 27 | `Time spent on ${showList ? 'unmounting' : 'mounting'} the subtree: ${ |
| 28 | afterRenderTime - startTime |
| 29 | }ms`, |
| 30 | ); |
| 31 | }); |
| 32 | }; |
| 33 | return ( |
| 34 | <div> |
| 35 | <h2>Mount/Unmount a large subtree</h2> |
| 36 | <p>Click the button to toggle the state. Open console for results.</p> |
| 37 | <button onClick={toggleList}>toggle</button> |
| 38 | <ul> |
| 39 | <li key="dummy">dummy item</li> |
| 40 | {showList && arr.map((num, idx) => <li key={idx}>{num}</li>)} |
| 41 | </ul> |
| 42 | </div> |
| 43 | ); |
| 44 | } |