-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
165 lines (148 loc) · 5.19 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Measure style and layout - demo</title>
<style>
/* the goal of these selectors is to be stupidly unperformant */
#component .table .tbody .row[even="true"]:nth-child(even) + .row[odd="true"]:nth-child(odd) {
font-size: 1.2em;
}
#component .table .tbody .row .cell:nth-child(even) {
background: aqua;
}
#component .table .tbody .row .cell:nth-child(odd) {
background: chocolate;
}
#component .table .tbody .row .cell:nth-child(even) + .cell:nth-child(odd) {
font-weight: bold;
}
#component .table .tbody .row .cell:nth-child(odd) + .cell:nth-child(even) {
font-size: 1.1em;
}
#component .table .tbody .row .cell[even="true"]:nth-child(even) + .cell[odd="true"]:nth-child(odd) {
font-weight: bold;
}
#component .table .tbody .row:nth-child(even) {
height: 40px;
overflow: hidden;
}
#component .table .tbody .row:nth-child(odd) {
height: 30px;
overflow: hidden;
}
</style>
</head>
<body>
<h1>Measure style and layout - demo</h1>
<button type="button" onclick="renderComponent(false, false)">Render component</button>
<button type="button" onclick="renderComponent(true, 'rAF+setTimeout')">Render component with rAF+setTimeout mark</button>
<button type="button" onclick="renderComponent(true, 'rAF+setTimeout', true)">Render component with rAF+setTimeout mark and gBCR</button>
<button type="button" onclick="renderComponent(true, 'rAF+Promise')">Render component with Promise microtask</button>
<button type="button" onclick="renderComponent(true, 'rAF+rAF')">Render component with double rAF</button>
<button type="button" onclick="renderComponent(true, 'rAF+rIC')">Render component with rAF + rIC</button>
<div id="component"></div>
<script>
(function () {
'use strict'
var $ = document.querySelector.bind(document)
var $component = $('#component')
function clearComponent () {
$component.innerHTML = ''
}
function createTable() {
var table = document.createElement('table')
table.classList.add('table')
var tbody = document.createElement('tbody')
tbody.classList.add('tbody')
table.appendChild(tbody)
for (var i = 0; i < 100; i++) {
var tr = document.createElement('tr')
tr.classList.add('row')
tr.setAttribute('odd', (i % 2 === 0).toString())
tr.setAttribute('even', (i % 2 === 1).toString())
for (var j = 0; j < 10; j++) {
var td = document.createElement('td')
td.classList.add('cell')
td.setAttribute('odd', (j % 2 === 0).toString())
td.setAttribute('even', (j % 2 === 1).toString())
td.textContent = 'Cell ' + (i + 1) + '/' + (j + 1)
tr.appendChild(td)
}
tbody.appendChild(tr)
}
return table
}
// do some useless work to emulate real-world websites
function doJavaScriptWork () {
function work () {
var obj = {}
for (var i = 0; i < 10; i++) {
obj[btoa(Math.random().toString())] = btoa(Math.random().toString())
}
for (var key in obj) {
obj[key] = obj[key] + btoa(Math.random().toString())
}
for (var key2 in obj) {
delete obj[key2]
}
window.obj = obj // avoid dead code elimination
}
for (var i = 0; i < 100; i++) {
work()
}
}
function renderComponent (startMark, endMark, callGBCR) {
if (startMark) {
performance.mark('start')
}
clearComponent()
doJavaScriptWork()
var table = createTable()
$component.appendChild(table)
if (callGBCR) {
window.myRect = $component.getBoundingClientRect() // avoid dead code elimination
}
if (endMark === 'rAF+setTimeout') {
requestAnimationFrame(function () {
performance.mark('rAF')
setTimeout(function () {
performance.mark('end')
performance.measure('total', 'start', 'end')
performance.measure('rAF to end', 'rAF', 'end')
})
})
} else if (endMark === 'rAF+Promise') {
requestAnimationFrame(function () {
performance.mark('rAF')
Promise.resolve().then(function () {
performance.mark('end')
performance.measure('total', 'start', 'end')
performance.measure('rAF to end', 'rAF', 'end')
})
})
} else if (endMark === 'rAF+rAF') {
requestAnimationFrame(function () {
performance.mark('rAF')
requestAnimationFrame(function () {
performance.mark('end')
performance.measure('total', 'start', 'end')
performance.measure('first rAF to end', 'rAF', 'end')
})
})
} else if (endMark === 'rAF+rIC') {
requestAnimationFrame(function () {
performance.mark('rAF')
requestIdleCallback(function () {
performance.mark('end')
performance.measure('total', 'start', 'end')
performance.measure('rAF to end', 'rAF', 'end')
})
})
}
}
window.renderComponent = renderComponent
})()
</script>
</body>
</html>