|
| 1 | +'use client' |
| 2 | +import { useState } from 'react' |
| 3 | +import useSWR from 'swr' |
| 4 | + |
| 5 | +const elementCount = 10_000 |
| 6 | +const useData = () => { |
| 7 | + return useSWR('1', async (url: string) => { |
| 8 | + return 1 |
| 9 | + }) |
| 10 | +} |
| 11 | + |
| 12 | +const HookUser = () => { |
| 13 | + const { data } = useData() |
| 14 | + return <div>{data}</div> |
| 15 | +} |
| 16 | +/** |
| 17 | + * This renders 10,000 divs and is used to compare against the render performance |
| 18 | + * when using swr. |
| 19 | + */ |
| 20 | +const CheapComponent = () => { |
| 21 | + const cheapComponents = Array.from({ length: 10_000 }, (_, i) => ( |
| 22 | + <div key={i}>{i}</div> |
| 23 | + )) |
| 24 | + return ( |
| 25 | + <div> |
| 26 | + <h2>Cheap Component</h2> |
| 27 | + {cheapComponents} |
| 28 | + </div> |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * This renders 10,000 divs, each of which uses the same swr hook. |
| 34 | + */ |
| 35 | +const ExpensiveComponent = () => { |
| 36 | + const hookComponents = Array.from({ length: 10_000 }, (_, i) => ( |
| 37 | + <HookUser key={i} /> |
| 38 | + )) |
| 39 | + return ( |
| 40 | + <div> |
| 41 | + <h2>Expensive Component</h2> |
| 42 | + {hookComponents} |
| 43 | + </div> |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +export default function PerformancePage() { |
| 48 | + const [renderExpensive, setRenderExpensive] = useState(false) |
| 49 | + return ( |
| 50 | + <div> |
| 51 | + <h1>Performance Page</h1> |
| 52 | + <label> |
| 53 | + <input |
| 54 | + type="checkbox" |
| 55 | + checked={renderExpensive} |
| 56 | + onChange={e => setRenderExpensive(e.target.checked)} |
| 57 | + /> |
| 58 | + Toggle Expensive Component |
| 59 | + </label> |
| 60 | + {!renderExpensive ? <CheapComponent /> : <ExpensiveComponent />} |
| 61 | + </div> |
| 62 | + ) |
| 63 | +} |
0 commit comments