Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 60de624

Browse files
committed
added 1000 fields update count
1 parent 1d37108 commit 60de624

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

benchmark/benchmarks/react/1000fields/bench/agilets/nestedState.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ const FIELDS = AgileApp.createState(
1111
)
1212
);
1313

14+
let updatedFieldsCount = 0;
15+
1416
function Field({ field }: { field: State<string> }) {
1517
const name = useAgile(field);
1618

19+
updatedFieldsCount++;
20+
1721
return (
1822
<div>
1923
Last {`<Field>`} render at: {new Date().toISOString()}
@@ -22,6 +26,9 @@ function Field({ field }: { field: State<string> }) {
2226
value={name}
2327
onChange={(e) => {
2428
field.set(e.target.value);
29+
(document.getElementById(
30+
'updatedFieldsCount'
31+
) as any).innerText = updatedFieldsCount;
2532
}}
2633
/>
2734
</div>
@@ -39,6 +46,7 @@ function App() {
3946
{fields.map((field, index) => (
4047
<Field key={index} field={field} />
4148
))}
49+
<div id={'updatedFieldsCount'} />
4250
</div>
4351
);
4452
}

benchmark/benchmarks/react/1000fields/bench/hookstate.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ const fields = createState(
66
Array.from(Array.from(Array(1000).keys()).map((i) => `Field #${i + 1} value`))
77
);
88

9+
let updatedFieldsCount = 0;
10+
911
function Field({ field }: { field: State<string> }) {
1012
const name = useHookstate(field);
1113

14+
updatedFieldsCount++;
15+
1216
return (
1317
<div>
1418
Last {`<Field>`} render at: {new Date().toISOString()}
@@ -17,6 +21,9 @@ function Field({ field }: { field: State<string> }) {
1721
value={name.get()}
1822
onChange={(e) => {
1923
name.set(e.target.value);
24+
(document.getElementById(
25+
'updatedFieldsCount'
26+
) as any).innerText = updatedFieldsCount;
2027
}}
2128
/>
2229
</div>
@@ -34,6 +41,7 @@ function App() {
3441
{state.map((field, index) => (
3542
<Field key={index} field={field} />
3643
))}
44+
<div id={'updatedFieldsCount'} />
3745
</div>
3846
);
3947
}

benchmark/benchmarks/react/1000fields/bench/jotai.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,26 @@ const fields = Array.from(Array(1000).keys()).map((i) =>
88

99
const fieldsStore = atom(fields);
1010

11+
let updatedFieldsCount = 0;
12+
1113
function Field({ field }: { field: Atom<string> }) {
1214
const [name, rename] = useAtom(field);
1315

16+
updatedFieldsCount++;
17+
1418
return (
1519
<div>
1620
Last {`<Field>`} render at: {new Date().toISOString()}
1721
&nbsp;
18-
<input value={name} onChange={(e) => rename(e.target.value)} />
22+
<input
23+
value={name}
24+
onChange={(e) => {
25+
rename(e.target.value);
26+
(document.getElementById(
27+
'updatedFieldsCount'
28+
) as any).innerText = updatedFieldsCount;
29+
}}
30+
/>
1931
</div>
2032
);
2133
}
@@ -32,6 +44,7 @@ function App() {
3244
{fields.map((field, index) => (
3345
<Field key={index} field={field} />
3446
))}
47+
<div id={'updatedFieldsCount'} />
3548
</div>
3649
);
3750
}

benchmark/benchmarks/react/1000fields/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ function configTest(renderElement: (target: HTMLElement) => void): Options {
4545
renderElement(target);
4646
},
4747
onComplete() {
48+
(this as any).updatedFieldsCount = parseInt(
49+
(document.getElementById('updatedFieldsCount') as any)?.innerText,
50+
10
51+
);
52+
4853
// Unmount React Component
4954
ReactDOM.unmountComponentAtNode(target);
5055
target.innerHTML = '';
@@ -71,7 +76,7 @@ suite
7176
.on('cycle', (event: any) => {
7277
console.log(
7378
String(event.target),
74-
`renderCount: ${event.target.renderCount}`
79+
`[updatedFieldsCount: ${event.target.updatedFieldsCount}]`
7580
);
7681
})
7782
.on('complete', function (this: any) {

benchmark/benchmarks/react/computed/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ function configTest(renderElement: (target: HTMLElement) => void): Options {
3636
},
3737
onComplete() {
3838
// Set 'output' in the Benchmark itself to print it later
39-
(this as any).output = parseInt(target.innerText, 10);
39+
(this as any).output = parseInt(
40+
(target.querySelector('h1') as any)?.innerText,
41+
10
42+
);
43+
(this as any).computedOutput = parseInt(
44+
(target.querySelector('p') as any)?.innerText,
45+
10
46+
);
4047

4148
// Unmount React Component
4249
ReactDOM.unmountComponentAtNode(target);
@@ -57,7 +64,10 @@ suite
5764
console.log(`Starting ${this.name}`);
5865
})
5966
.on('cycle', (event: any) => {
60-
console.log(String(event.target));
67+
console.log(
68+
String(event.target),
69+
`[Count: ${event.target.output}, ComputedCount: ${event.target.computedOutput}]`
70+
);
6171
})
6272
.on('complete', function (this: any) {
6373
console.log(`Fastest is ${this.filter('fastest').map('name')}`);

benchmark/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"test": "node -r esbuild-register run.ts",
1111
"test:counter": "yarn test ./benchmarks/react/counter",
12-
"test:fields": "yarn test ./benchmarks/react/1000fields",
12+
"test:1000fields": "yarn test ./benchmarks/react/1000fields",
1313
"test:computed": "yarn test ./benchmarks/react/computed",
1414
"install:local:agile": "yalc add @agile-ts/core @agile-ts/react",
1515
"install:public:agile": "yarn add @agile-ts/core @agile-ts/react"

0 commit comments

Comments
 (0)