-
Notifications
You must be signed in to change notification settings - Fork 889
Expand file tree
/
Copy pathbenchmark.ts
More file actions
190 lines (175 loc) · 4.86 KB
/
benchmark.ts
File metadata and controls
190 lines (175 loc) · 4.86 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { defineComponent, signal } from 'thane';
const adjectives = [
'pretty',
'large',
'big',
'small',
'tall',
'short',
'long',
'handsome',
'plain',
'quaint',
'clean',
'elegant',
'easy',
'angry',
'crazy',
'helpful',
'mushy',
'odd',
'unsightly',
'adorable',
'important',
'inexpensive',
'cheap',
'expensive',
'fancy',
];
const colours = ['red', 'yellow', 'blue', 'green', 'pink', 'brown', 'purple', 'brown', 'white', 'black', 'orange'];
const nouns = [
'table',
'chair',
'house',
'bbq',
'desk',
'car',
'pony',
'cookie',
'sandwich',
'burger',
'pizza',
'mouse',
'keyboard',
];
const pick = (arr: string[]) => arr[Math.round(Math.random() * 1000) % arr.length]!;
const buildLabel = () => `${pick(adjectives)} ${pick(colours)} ${pick(nouns)}`;
interface RowData {
id: number;
label: string;
}
let nextId = 1;
const buildData = (count: number): RowData[] => {
const data: RowData[] = new Array(count);
for (let i = 0; i < count; i++) {
data[i] = { id: nextId++, label: buildLabel() };
}
return data;
};
export const Benchmark = defineComponent('bench-mark', () => {
const rows = signal<RowData[]>([]);
const selected = signal<number>(0);
const select = (id: number) => {
selected(id);
};
const run = () => {
rows(buildData(1000));
selected(0);
};
const runLots = () => {
rows(buildData(10000));
selected(0);
};
const add = () => {
rows([...rows(), ...buildData(1000)]);
};
const update = () => {
const data = rows();
const newData = [...data];
for (let i = 0; i < newData.length; i += 10) {
const item = newData[i]!;
newData[i] = { ...item, label: item.label + ' !!!' };
}
rows(newData);
};
const clear = () => {
rows([]);
selected(0);
};
const swapRows = () => {
const data = rows();
if (data.length > 998) {
const newData = [...data];
const temp = newData[1]!;
newData[1] = newData[998]!;
newData[998] = temp;
rows(newData);
}
};
const remove = (id: number) => {
const data = rows();
const idx = data.findIndex((d) => d.id === id);
if (idx !== -1) {
rows(data.slice(0, idx).concat(data.slice(idx + 1)));
}
};
return {
template: html`
<div class="container">
<div class="jumbotron">
<div class="row">
<div class="col-md-6">
<h1>Thane Benchmark</h1>
</div>
<div class="col-md-6">
<div class="row">
<div class="col-sm-6 smallpad">
<button type="button" class="btn btn-primary btn-block" id="run" @click=${run}
>Create 1,000 rows</button
>
</div>
<div class="col-sm-6 smallpad">
<button type="button" class="btn btn-primary btn-block" id="runlots" @click=${runLots}
>Create 10,000 rows</button
>
</div>
<div class="col-sm-6 smallpad">
<button type="button" class="btn btn-primary btn-block" id="add" @click=${add}
>Append 1,000 rows</button
>
</div>
<div class="col-sm-6 smallpad">
<button type="button" class="btn btn-primary btn-block" id="update" @click=${update}
>Update every 10th row</button
>
</div>
<div class="col-sm-6 smallpad">
<button type="button" class="btn btn-primary btn-block" id="clear" @click=${clear}>Clear</button>
</div>
<div class="col-sm-6 smallpad">
<button type="button" class="btn btn-primary btn-block" id="swaprows" @click=${swapRows}
>Swap Rows</button
>
</div>
</div>
</div>
</div>
</div>
<table class="table table-hover table-striped test-data">
<tbody id="tbody">
${repeat(
rows(),
(row) => html`
<tr class=${selected() === row.id ? 'danger' : ''}>
<td class="col-md-1">${row.id}</td>
<td class="col-md-4">
<a @click=${() => select(row.id)}>${row.label}</a>
</td>
<td class="col-md-1">
<a @click=${() => remove(row.id)}>
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>
</td>
<td class="col-md-6"></td>
</tr>
`,
null,
(row) => row.id,
)}
</tbody>
</table>
<span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
</div>
`,
};
});