/
todo.html
247 lines (230 loc) · 5.87 KB
/
todo.html
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ToDo List</title>
<style>
body {
padding: 20px;
font-family: system-ui;
}
a {
opacity: 0.5;
}
h1 {
margin-top: 0;
font-size: 150%;
font-weight: inherit;
}
ul {
list-style: none;
padding: 0;
}
li {
display: flex;
padding: 0 10px;
align-items: center;
}
li:nth-child(odd) {
background-color: #f0f6ff;
}
li > * {
display: inline-block;
flex: 0;
padding: 5px;
margin: 0;
}
li p {
flex: 1;
}
li[done] p {
opacity: 0.5;
text-decoration: line-through;
}
</style>
</head>
<body>
<div id="app"></div>
<script type="module">
import {
mutateAndLayoutAsync,
sleep,
measureName,
measureMemory
} from './util.js';
import { createRoot, createElement as h, Component } from 'framework';
// Number of warmup runs of the benchmark to execute before the timed run
const WARMUP_COUNT = 5;
// Number of ToDo list items to render/toggle/delete
// NOTE: *must* be divisible by 4.
const NUM_ITEMS = 40;
const freshState = () => ({ counter: 0, text: '', todos: [] });
let state = freshState();
function mutation(fn) {
return e => {
rerender((state = Object.assign({}, state, fn(state, e))));
};
}
const add = mutation(({ counter, text, todos }, e) => {
e.preventDefault();
const id = ++counter;
return { counter, text: '', todos: todos.concat({ text, id }) };
});
const setText = mutation((state, e) => ({ text: e.target.value }));
const toggle = mutation(({ todos }, e) => {
const id = e.currentTarget.getAttribute('data-todo');
todos = todos.map(todo =>
todo.id == id ? { ...todo, done: !todo.done } : todo
);
return { todos };
});
const remove = mutation(({ todos }, e) => {
const id = e.currentTarget.getAttribute('data-todo');
todos = todos.filter(todo => todo.id != id);
return { todos };
});
function TodoItem({ todo }) {
return h(
'li',
{
done: todo.done,
'data-todo': todo.id,
onClick: toggle
},
h('input', {
type: 'checkbox',
checked: todo.done,
readonly: true
}),
h('p', null, todo.text),
h('a', { 'data-todo': todo.id, onClick: remove }, '✕')
);
}
function App({ text, todos }) {
return h(
'div',
null,
h(
'form',
{ onSubmit: add },
h('input', {
value: text,
onInput: setText,
placeholder: 'Enter a new to-do item...'
}),
h('button', { type: 'submit', disabled: !text }, 'Add')
),
h(
'ul',
null,
todos.map(todo => h(TodoItem, { key: todo.id, todo }))
)
);
}
const root = createRoot(document.getElementById('app'));
function rerender() {
root.render(h(App, state));
}
rerender();
const BUBBLING_EVENT = {};
function type(el, text) {
const OPTS = {
inputType: 'inserting',
data: '',
bubbles: true,
cancelable: true
};
let value = '';
for (let i = 0; i < text.length; i++) {
const ch = text[i];
value += ch;
OPTS.data = ch;
el.value = value;
el.dispatchEvent(new InputEvent('input', OPTS));
}
el.dispatchEvent(new InputEvent('change', OPTS));
}
const $ = sel => document.querySelector(sel);
function runPatch() {
state = freshState();
rerender();
const input = $('input');
const form = $('form');
const list = $('ul');
const button = $('button');
for (let i = 1; i <= NUM_ITEMS; i++) {
type(input, `Item ${i}`);
button.click();
const itemsInDom = list.children.length;
if (itemsInDom !== i) {
throw Error(`Expected ${i} ToDo list items, got ${itemsInDom}.`);
}
}
// this check also forces layout in order to include that time in test measured time:
if (list.offsetHeight < NUM_ITEMS * 5) {
throw Error(
`Expected list to have height > ${NUM_ITEMS * 5}, got ${
list.offsetHeight
}.`
);
}
const items = [].slice.call(list.children);
for (let i = 0; i < items.length; i++) {
items[i].click();
}
if (!items.every(item => item.hasAttribute('done'))) {
throw Error(`Expected all items to have [done] attribute.`);
}
for (let i = 0; i < items.length; i++) {
items[i].click();
}
if (items.some(item => item.hasAttribute('done'))) {
throw Error(
`Expected [done] attribute to be removed from all items.`
);
}
const count = NUM_ITEMS / 4;
for (let i = count; i < count * 3; i++) {
items[i].lastElementChild.click();
}
for (let i = 0; i < count; i++) {
items[i].lastElementChild.click();
}
for (let i = count * 4; i-- > count * 3; ) {
items[i].lastElementChild.click();
}
if (items.some(item => item.isConnected)) {
throw Error(`Expected all items to be removed from the DOM.`);
}
if (list.offsetHeight > 10) {
throw Error(
`Expected empty list to have a height of approximately 0px.`
);
}
root.render(null);
if ($('#app').children.length > 0) {
throw Error(`Expected entire application to be un-rendered.`);
}
}
async function warmup() {
for (let i = 0; i < WARMUP_COUNT; i++) {
await runPatch();
await new Promise(r => requestAnimationFrame(r));
}
}
warmup().then(async () => {
await sleep(200);
// This triggers a rAF, then runs a synchronous benchmark followed by one "tick",
// which should include the cost of layout in all current browsers.
await mutateAndLayoutAsync(() => {
performance.mark('start');
runPatch();
});
performance.mark('stop');
performance.measure(measureName, 'start', 'stop');
measureMemory();
});
</script>
</body>
</html>