summaryrefslogtreecommitdiff
path: root/preact/benches/src/filter_list.html
blob: d31e1aff655dece50cc1591797c0405221f2d649 (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
<!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>
			.items {
				margin: 1em 0;
				padding: 0;
				display: flex;
				flex-wrap: wrap;
				gap: 2px;
			}

			.items > * {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 3em;
				height: 2em;
				margin: 0;
				padding: 0;
				background: #eee;
			}
		</style>
	</head>
	<body>
		<div id="app"></div>
		<script type="module">
			import { measureName, measureMemory } from './util.js';
			import { createRoot, createElement as h, Component } from 'framework';

			function Row(props) {
				return h('article', null, props.children);
			}

			function App(props) {
				return h('div', {}, [
					h(
						'div',
						{ class: 'items' },
						props.items.map(id => h(Row, { key: id }, id))
					)
				]);
			}

			const count = 1000;
			const start = 20;
			const end = 600;

			const newItems = () =>
				Array(count)
					.fill(0)
					.map((item, i) => i);
			let items = newItems();
			let currentItems = items;

			const root = createRoot(document.getElementById('app'));
			root.render(h(App, { items }));

			function runPatch() {
				items = newItems().filter(id => {
					const isVisible = currentItems.includes(id);
					return id >= start && id <= end ? !isVisible : isVisible;
				});
				currentItems = items;

				root.render(h(App, { items }));
			}

			async function warmup() {
				const count = 25;

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

			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>