-
Notifications
You must be signed in to change notification settings - Fork 106
/
performance-demo-large-table.tsx
145 lines (138 loc) · 6.02 KB
/
performance-demo-large-table.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { useEffect } from 'react';
import { useStateLink, StateLink, Downgraded } from '@hookstate/core';
const TableCell = (props: { cellState: StateLink<number> }) => {
const state = useStateLink(props.cellState);
return <>{state.value.toString(16)}</>;
}
const MatrixView = (props: { totalRows: number, totalColumns: number, interval: number, callsPerInterval: number }) => {
const totalRows = props.totalRows;
const totalColumns = props.totalColumns;
// we use local per component state,
// but the same result would be for the global state
// if it was created by createStateLink
const matrixState = useStateLink(
Array.from(Array(totalRows).keys())
.map(i => Array.from(Array(totalColumns).keys()).map(j => 0)));
// schedule interval updates
useEffect(() => {
const t = setInterval(() => {
function randomInt(min: number, max: number) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
for (let i = 0; i < props.callsPerInterval; i += 1) {
matrixState
.nested[randomInt(0, totalRows)]
.nested[randomInt(0, totalColumns)]
.set(p => p + randomInt(0, 5))
}
}, props.interval)
return () => clearInterval(t);
})
return <div style={{ overflow: 'scroll' }}>
<PerformanceMeter matrixState={matrixState} />
<table
style={{
border: 'solid',
borderWidth: 1,
borderColor: 'grey',
color: '#00FF00',
backgroundColor: 'black'
}}
>
<tbody>
{matrixState.nested.map((rowState, rowIndex: number) =>
<tr key={rowIndex}>
{rowState.nested.map((cellState, columnIndex) =>
<td key={columnIndex}><TableCell cellState={cellState}/></td>
)}
</tr>
)}
</tbody>
</table>
</div>
}
export const ExampleComponent = () => {
const settings = useStateLink({
totalRows: 50,
totalColumns: 50,
rate: 50,
timer: 10
}, (s) => ({
totalRows: s.value.totalRows,
totalColumns: s.value.totalColumns,
rate: s.value.rate,
timer: s.value.timer,
setRows: (f: (p: number) => number) => s.nested.totalRows.set(f),
setColumns: (f: (p: number) => number) => s.nested.totalColumns.set(f),
setRate: (f: (p: number) => number) => s.nested.rate.set(f),
setTimer: (f: (p: number) => number) => s.nested.timer.set(f),
}));
return <>
<div>
<p><span>Total rows: {settings.totalRows} </span>
<button onClick={() => settings.setRows(p => (p - 10) || 10)}>-10</button>
<button onClick={() => settings.setRows(p => p + 10)}>+10</button></p>
<p><span>Total columns: {settings.totalColumns} </span>
<button onClick={() => settings.setColumns(p => (p - 10) || 10)}>-10</button>
<button onClick={() => settings.setColumns(p => p + 10)}>+10</button></p>
<p>Total cells: {settings.totalColumns * settings.totalRows}</p>
<p><span>Cells to update per timer interval: {settings.rate} </span>
<button onClick={() => settings.setRate(p => (p - 1) || 1)}>-1</button>
<button onClick={() => settings.setRate(p => p + 1)}>+1</button>
<button onClick={() => settings.setRate(p => p > 10 ? (p - 10) : 1)}>-10</button>
<button onClick={() => settings.setRate(p => p + 10)}>+10</button>
</p>
<p><span>Timer interval in ms: {settings.timer} </span>
<button onClick={() => settings.setTimer(p => p > 1 ? (p - 1) : 1)}>-1</button>
<button onClick={() => settings.setTimer(p => p + 1)}>+1</button>
<button onClick={() => settings.setTimer(p => p > 10 ? (p - 10) : 1)}>-10</button>
<button onClick={() => settings.setTimer(p => p + 10)}>+10</button>
</p>
</div>
<MatrixView
key={Math.random()}
totalRows={settings.totalRows}
totalColumns={settings.totalColumns}
interval={settings.timer}
callsPerInterval={settings.rate}
/>
</>;
}
const PerformanceViewPluginID = Symbol('PerformanceViewPlugin');
function PerformanceMeter(props: { matrixState: StateLink<number[][]> }) {
const stats = React.useRef({
totalSum: 0,
totalCalls: 0,
startTime: (new Date()).getTime()
})
const elapsedMs = () => (new Date()).getTime() - stats.current.startTime;
const elapsed = () => Math.floor(elapsedMs() / 1000);
const rate = Math.floor(stats.current.totalCalls / elapsedMs() * 1000);
const scopedState = useStateLink(props.matrixState)
.with(() => ({
id: PerformanceViewPluginID,
instanceFactory: () => ({
onPreset: (path, prevState, newCellValue) => {
if (path.length === 2) {
// new value can be only number in this example
// and path can contain only 2 elements: row and column indexes
stats.current.totalSum += newCellValue - prevState[path[0]][path[1]];
}
},
onSet: () => {
stats.current.totalCalls += 1;
}
})
}))
// mark the value of the whole matrix as 'used' by this component
scopedState.with(Downgraded);
scopedState.get();
return <>
<p><span>Elapsed: {elapsed()}s</span></p>
<p><span>Total cells sum: {stats.current.totalSum}</span></p>
<p><span>Total matrix state updates: {stats.current.totalCalls}</span></p>
<p><span>Average update rate: {rate}cells/s</span></p>
</>;
}