Skip to content

Commit b1583d6

Browse files
committed
Add an e2e test page for testing the swr performance when many hooks are used.
1 parent aeb9363 commit b1583d6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

e2e/site/app/perf/page.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)