-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.html
203 lines (185 loc) · 6.04 KB
/
benchmark.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
<html>
<html lang="en">
<head>
<title>Benchmark - inline style</title>
<style>
:root {
--background-parent: red;
--background-child: blue;
}
button {
margin: 10px;
}
body {
display: flex;
flex-wrap: wrap;
align-items: center;
}
body > div {
flex: 1;
height: 50px;
width: 50px;
background-color: var(--background-parent);
}
div > div {
color: white;
margin: 5px;
width: 40px;
height: 40px;
background-color: var(--background-child);
}
hr {
flex: 0 100%;
}
.parent-green-cssvar { --background-parent: green; }
.child-green-cssvar { --background-child: green; }
.parent-green-raw { background: green; }
.child-green-raw { background: green; }
</style>
</head>
<body>
<span>Classname (raw value):</span>
<button onclick="changeParentsClassNameRawValue()">parent</button>
<button onclick="changeChildrenClassNameRawValue()">children</button>
<hr />
<span>Classname (CSS var):</span>
<button onclick="changeParentsClassNameCSSVar()">parent</button>
<button onclick="changeChildrenClassNameCSSVar()">children</button>
<hr />
<span>Document CSS var: </span>
<button onclick="changeParentsDocumentCSSVar()">parent</button>
<button onclick="changeChildrenDocumentCSSVar()">children</button>
<hr />
<span>Style attribute (raw value):</span>
<button onclick="changeParentsStyleAttrRawValue()">parent</button>
<button onclick="changeChildrenStyleAttrRawValue()">children</button>
<hr />
<span>Style attribute (CSS var):</span>
<button onclick="changeParentsStyleAttrCSSVar()">parent</button>
<button onclick="changeChildrenStyleAttrCSSVar()">children</button>
<hr />
<span>Inject style tag: </span>
<button onclick="injectParentStyleTag()">parent</button>
<button onclick="injectChildStyleTag()">children</button>
<hr />
<span>Insert rule (raw value): </span>
<button onclick="insertRuleParentRawValue()">parent</button>
<button onclick="insertRuleChildRawValue()">children</button>
<hr />
<span>Insert rule (CSS var): </span>
<button onclick="insertRuleParentCSSVar()">parent</button>
<button onclick="insertRuleChildCSSVar()">children</button>
<hr />
<script>
// Class raw value
function changeParentsClassNameRawValue() {
document.querySelectorAll('body > div').forEach(div => {
div.className = 'parent-green-raw'
})
}
function changeChildrenClassNameRawValue() {
document.querySelectorAll('div div').forEach(div => {
div.className = 'child-green-raw'
})
}
// Class CSS var
function changeParentsClassNameCSSVar() {
document.querySelectorAll('body > div').forEach(div => {
div.className = 'parent-green-cssvar'
})
}
function changeChildrenClassNameCSSVar() {
document.querySelectorAll('div div').forEach(div => {
div.className = 'child-green-cssvar'
})
}
// Document CSS var
function changeParentsDocumentCSSVar() {
document.documentElement.style.setProperty('--background-parent', 'green')
}
function changeChildrenDocumentCSSVar() {
document.documentElement.style.setProperty('--background-child', 'green')
}
// Style attribute raw value
function changeParentsStyleAttrRawValue() {
document.querySelectorAll('body > div').forEach(div => {
div.style.background = 'green'
})
}
function changeChildrenStyleAttrRawValue() {
document.querySelectorAll('div div').forEach(div => {
div.style.background = 'green'
})
}
// Style attribute CSS var
function changeParentsStyleAttrCSSVar() {
document.querySelectorAll('body > div').forEach(div => {
div.style.setProperty('--background-parent', 'green')
})
}
function changeChildrenStyleAttrCSSVar() {
document.querySelectorAll('div div').forEach(div => {
div.style.setProperty('--background-child', 'green')
})
}
// Inject style tag
function injectParentStyleTag() {
for (let i = 1; i <= 5000; i++) {
const style = document.createElement('style')
style.textContent += `.x${i} { background-color: green }`
document.head.appendChild(style)
}
}
function injectChildStyleTag() {
for (let i = 1; i <= 5000; i++) {
const style = document.createElement('style')
style.textContent += `.x${i} > div { background-color: green }`
document.head.appendChild(style)
}
}
// Use insertRule
function insertRuleParentRawValue() {
const sheet = new CSSStyleSheet()
for (let i = 1; i <= 5000; i++) {
sheet.insertRule(`.x${i} { background-color: green }`)
}
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]
}
function insertRuleChildRawValue() {
const sheet = new CSSStyleSheet()
for (let i = 1; i <= 5000; i++) {
sheet.insertRule(`.x${i} > div { background-color: green }`)
}
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]
}
// Use insertRule with CSS var
function insertRuleParentCSSVar() {
const sheet = new CSSStyleSheet()
for (let i = 1; i <= 5000; i++) {
sheet.insertRule(`.x${i} { --background-parent: green }`)
}
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]
}
function insertRuleChildCSSVar() {
const sheet = new CSSStyleSheet()
for (let i = 1; i <= 5000; i++) {
sheet.insertRule(`.x${i} > div { --background-child: green }`)
}
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]
}
// Benchmark
function renderDivs() {
const container = document.body
for (let i = 1; i <= 5000; i++) {
const div = document.createElement('div')
div.classList.add(`x${i}`)
container.appendChild(div)
const child = document.createElement('div')
child.textContent = i
div.appendChild(child)
}
}
renderDivs()
</script>
</body>
</html>