summaryrefslogtreecommitdiff
path: root/preact/benches/src/many_updates.html
blob: e59b30421f3dde7bdc91a3e45d23910f6e5f576f (plain)
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
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Patching HTML</title>
		<style>
			.hello {
				color: red;
			}

			.bye {
				color: blue;
			}
		</style>
	</head>
	<body>
		<div id="root"></div>
		<script type="module">
			import { measureName, measureMemory } from './util.js';
			import { createRoot, createElement as h } from 'framework';

			const state = {
				msg: 'hello',
				list: new Array(1000).fill(0).map((_, i) => ({
					i,
					text: 'foobar' + i
				}))
			};

			let counter = 0;
			function App() {
				return h(
					'div',
					{ id: 'app' },
					h('p', null, '> ', ++counter, ' <'),
					h('p', null, state.msg),
					...state.list.map((obj, i) =>
						h(
							'div',
							{ key: i, title: state.msg + i },
							h('span', { className: state.msg }, obj.text),
							h('span', { className: 'baz' }, 'one'),
							h('span', { className: 'qux' }, 'two'),
							h(
								'div',
								null,
								h('span', { className: 'qux' }, 'three'),
								h('span', { className: 'qux' }, 'four'),
								h('span', { className: 'baz' }, 'five'),
								h(
									'div',
									null,
									h('span', { className: 'qux' }, 'six'),
									h('span', { className: 'baz' }, 'seven'),
									h('span', { className: state.msg }, 'eight')
								)
							)
						)
					)
				);
			}

			const root = createRoot(document.getElementById('root'));

			// const p = performance.now();
			root.render(h(App));
			// console.log(`mount: ${(performance.now() - p).toFixed(2)}ms`);

			// const patchResults = [];

			function runPatch() {
				// const s = performance.now();
				state.msg = state.msg === 'hello' ? 'bye' : 'hello';
				state.list[0].text = state.msg;
				root.render(h(App));
				// patchResults.push(performance.now() - s);
			}

			async function warmup() {
				// const count = 100;
				const count = 25;

				for (let i = 0; i < count; i++) {
					runPatch();
					await new Promise(r => requestAnimationFrame(r));
				}

				// let fastest = Infinity;
				// const total = patchResults.reduce((all, cur) => {
				// 	if (cur < fastest) {
				// 		fastest = cur;
				// 	}
				// 	return all + cur;
				// }, 0);

				// console.log(`${count} runs average: ${(total / count).toFixed(2)}ms`);
				// console.log(`fastest run: ${fastest.toFixed(2)}ms`);
			}

			warmup().then(async () => {
				performance.mark('start');
				runPatch();
				await new Promise(r => requestAnimationFrame(r));
				performance.mark('stop');
				performance.measure(measureName, 'start', 'stop');

				measureMemory();
			});
		</script>
	</body>
</html>