page.tsx
Overview
page.tsx is a React component file designed to benchmark rendering performance differences between two approaches of rendering a large number of components (10,000 in this case). It contrasts:
Rendering many simple
<div>elements directly (the CheapComponent)Rendering many components that each use the SWR data fetching hook (the ExpensiveComponent)
The file exports a main component, PerformancePage, which allows the user to toggle between rendering the cheap or expensive component sets. This enables developers to observe and compare the rendering cost of using hooks like SWR at scale.
Components and Functions
useData
const useData = () => {
return useSWR('1', async (url: string) => {
return 1
})
}
Purpose: Custom hook using
useSWRto fetch data for a fixed key'1'.Parameters: None.
Returns: The SWR response object containing fields like
data,error,isLoading.Behavior: Returns a constant value
1asynchronously for any request to key'1'.Usage Example:
const { data } = useData();
console.log(data); // 1
Implementation Detail:
The fetcher function is trivial, always resolving to1. This simulates a data fetching hook without network latency or complexity.
HookUser
const HookUser = () => {
const { data } = useData();
return <div>{data}</div>;
}
Purpose: A simple component that uses the
useDatahook and renders the fetcheddata.Parameters: None.
Returns: JSX element rendering the
datavalue inside a<div>.Usage Example:
<HookUser /> // Renders: <div>1</div>
Note: This component is used repeatedly in the expensive rendering test.
CheapComponent
const CheapComponent = () => {
const cheapComponents = Array.from({ length: elementCount }, (_, i) => (
<div key={i}>{i}</div>
));
return (
<div>
<h2>Cheap Component</h2>
{cheapComponents}
</div>
)
}
Purpose: Render 10,000 simple
<div>elements, each displaying its index.Parameters: None.
Returns: JSX element containing a heading and 10,000 numbered
<div>s.Implementation Detail:
UsesArray.fromwithelementCount(10,000) to create an array of<div>elements with keys.Usage Example:
<CheapComponent />
This component benchmarks rendering a large number of simple elements without hooks.
ExpensiveComponent
const ExpensiveComponent = () => {
const hookComponents = Array.from({ length: elementCount }, (_, i) => (
<HookUser key={i} />
));
return (
<div>
<h2>Expensive Component</h2>
{hookComponents}
</div>
)
}
Purpose: Render 10,000 instances of
HookUser, each using theuseDatahook.Parameters: None.
Returns: JSX containing a heading and 10,000
HookUsercomponents.Implementation Detail:
Each child independently calls the SWR hook, simulating a heavy rendering scenario where many components rely on asynchronous data fetching hooks.Usage Example:
<ExpensiveComponent />
This component benchmarks the cost of rendering many hook-using components simultaneously.
PerformancePage (default export)
export default function PerformancePage() {
const [renderExpensive, setRenderExpensive] = useState(false);
return (
<div>
<h1>Performance Page</h1>
<label>
<input
type="checkbox"
checked={renderExpensive}
onChange={e => setRenderExpensive(e.target.checked)}
/>
Render with swr
</label>
{!renderExpensive ? <CheapComponent /> : <ExpensiveComponent />}
</div>
)
}
Purpose: Main page component that toggles between rendering
CheapComponentorExpensiveComponent.Parameters: None.
Returns: JSX element rendering:
A heading (
<h1>Performance Page</h1>)A checkbox controlling which component set to render
Conditionally renders either
CheapComponent(default) orExpensiveComponent(when checkbox is checked).
Usage Example:
<PerformancePage />
Behavior:
When checkbox is unchecked: renders 10,000 simple divs.
When checked: renders 10,000 components each using
useDatahook.
Purpose: Allows interactive performance testing of rendering strategies.
Important Implementation Details / Algorithms
High Volume Rendering: Both
CheapComponentandExpensiveComponentrender 10,000 child elements/components to test React rendering performance under heavy load.SWR Caching: Although 10,000
HookUsercomponents calluseData, SWR’s internal caching mechanism ensures the data fetch for key'1'happens only once, avoiding redundant network requests. However, the overhead comes from 10,000 hook calls and React re-rendering.Key Prop Usage: Each child component in the arrays has a unique
keyprop to help React efficiently reconcile the lists.React Client Component: The
'use client'directive at the top marks this as a client-side React component in Next.js 13+, enabling hooks and interactivity.
Interaction with Other Parts of the System
Dependency on
useSWR: This file uses theuseSWRhook from theswrpackage for data fetching and caching.React State: Uses React's
useStateto control rendering mode.Intended Use: This file likely serves as a standalone benchmarking or demonstration page within a larger Next.js or React application, focused on performance testing of component rendering strategies.
No external APIs or databases: The SWR fetcher is a stub returning
1, implying no backend integration within this file.
Visual Diagram
componentDiagram
PerformancePage --|> CheapComponent : renders (if unchecked)
PerformancePage --|> ExpensiveComponent : renders (if checked)
ExpensiveComponent *-- HookUser : renders 10,000 instances
HookUser *-- useData : uses SWR hook
useData ..> useSWR : calls
Diagram Explanation:
PerformancePageconditionally renders eitherCheapComponentorExpensiveComponent.ExpensiveComponentrenders manyHookUsercomponents.Each
HookUsercallsuseData.useDatainternally calls the SWR hookuseSWRfor data fetching.
Summary
page.tsx provides an interactive performance testing page that contrasts rendering many simple elements versus many hook-based components. It leverages React state, SWR caching, and bulk rendering to highlight the cost differences in UI rendering strategies. This file is useful for developers to empirically evaluate rendering overhead in React applications using hooks and large component trees.