Skip to content

Commit f95340a

Browse files
authored
avoid getting property basenames if we can (#226)
1 parent 0773f88 commit f95340a

File tree

3 files changed

+63
-63
lines changed

3 files changed

+63
-63
lines changed

benchmark/readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ Trello.com (312 kB): 13.15 ops/sec
2424
====================================================================
2525
File | Size | total | parse | Analyze |
2626
====================================================================
27-
Bol.com | 468 kB | 328ms | 205ms | 122ms (37.2%) |
28-
Bootstrap 5.0.0 | 195 kB | 142ms | 102ms | 40ms (28.2%) |
29-
CSS-Tricks | 195 kB | 113ms | 60ms | 53ms (46.9%) |
30-
Facebook.com | 268 kB | 132ms | 69ms | 62ms (47.0%) |
31-
GitHub.com | 514 kB | 203ms | 114ms | 87ms (42.9%) |
32-
Gazelle.nl | 972 kB | 366ms | 228ms | 134ms (36.6%) |
33-
Lego.com | 246 kB | 86ms | 38ms | 48ms (55.8%) |
34-
Smashing Magazine.com | 1.1 MB | 428ms | 219ms | 208ms (48.6%) |
35-
Trello.com | 312 kB | 131ms | 72ms | 59ms (45.0%) |
27+
Bol.com | 468 kB | 328ms | 207ms | 120ms (36.6%) |
28+
Bootstrap 5.0.0 | 195 kB | 120ms | 80ms | 39ms (32.5%) |
29+
CSS-Tricks | 195 kB | 96ms | 64ms | 32ms (33.3%) |
30+
Facebook.com | 268 kB | 134ms | 78ms | 56ms (41.8%) |
31+
GitHub.com | 514 kB | 172ms | 91ms | 79ms (45.9%) |
32+
Gazelle.nl | 972 kB | 363ms | 225ms | 134ms (36.9%) |
33+
Lego.com | 246 kB | 77ms | 40ms | 36ms (46.8%) |
34+
Smashing Magazine.com | 1.1 MB | 350ms | 210ms | 139ms (39.7%) |
35+
Trello.com | 312 kB | 92ms | 52ms | 40ms (43.5%) |
3636
```

benchmark/run.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import byteSize from './format-filesize.js'
22
import { analyze as analyzeCss } from '../dist/analyzer.modern.js'
33
import * as fs from 'fs'
44
const files = [
5-
['bol-com-20190617', 'Bol.com', 127],
5+
['bol-com-20190617', 'Bol.com', 117],
66
['bootstrap-5.0.0', 'Bootstrap 5.0.0', 49],
77
['css-tricks-20190319', 'CSS-Tricks', 50],
8-
['facebook-20190319', 'Facebook.com', 75],
8+
['facebook-20190319', 'Facebook.com', 71],
99
['github-20210501', 'GitHub.com', 95],
10-
['gazelle-20210905', 'Gazelle.nl', 325],
11-
['lego-20190617', 'Lego.com', 62],
12-
['smashing-magazine-20190319', 'Smashing Magazine.com', 300],
13-
['trello-20190617', 'Trello.com', 86]
10+
['gazelle-20210905', 'Gazelle.nl', 312],
11+
['lego-20190617', 'Lego.com', 59],
12+
['smashing-magazine-20190319', 'Smashing Magazine.com', 285],
13+
['trello-20190617', 'Trello.com', 80]
1414
]
1515

1616
let maxLen = -1
@@ -35,7 +35,7 @@ files.forEach(([filename, name, expectedDuration]) => {
3535
])
3636
})
3737

38-
const RUN_COUNT = 20
38+
const RUN_COUNT = 25
3939

4040
suite.forEach(([name, fn, expectedDuration]) => {
4141
const start = new Date()
@@ -47,6 +47,6 @@ suite.forEach(([name, fn, expectedDuration]) => {
4747
console.log(
4848
name,
4949
`${duration}ms`.padStart(6, ' '),
50-
`(${overtime >= 0 ? '-' : '+'}${Math.abs(overtime)}ms ${Math.round(Math.abs(overtime) / duration * 100)}%)`
50+
`(${overtime >= 0 ? '-' : '+'}${Math.abs(overtime)}ms ${Math.round(Math.abs(overtime) / duration * 100)}%)`,
5151
)
5252
})

src/index.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -239,63 +239,63 @@ const analyze = (css) => {
239239
}
240240

241241
const { value, property } = node
242-
const fullProperty = getProperty(property)
243242

244243
properties.push(property)
244+
values.push(value)
245245

246-
if (fullProperty.vendor) {
246+
// Process properties first that don't have colors,
247+
// so we can avoid further walking them;
248+
// They're also typically not vendor prefixed,
249+
if (property === 'z-index') {
250+
zindex.push(value)
251+
return this.skip
252+
}
253+
if (property === 'font') {
254+
fontValues.push(value)
255+
break
256+
}
257+
if (property === 'font-size') {
258+
fontSizeValues.push(stringifyNode(value))
259+
break
260+
}
261+
if (property === 'font-family') {
262+
fontFamilyValues.push(stringifyNode(value))
263+
break
264+
}
265+
266+
const {
267+
vendor: isVendor,
268+
hack: isHack,
269+
custom: isCustom,
270+
basename,
271+
} = getProperty(property)
272+
273+
if (isVendor) {
247274
propertyVendorPrefixes.push(property)
248275
}
249-
if (fullProperty.hack) {
276+
if (isHack) {
250277
propertyHacks.push(property)
251278
}
252-
if (fullProperty.custom) {
279+
if (isCustom) {
253280
customProperties.push(property)
254281
}
255282

256-
values.push(value)
283+
if (basename === 'transition' || basename === 'animation') {
284+
animations.push(value.children)
285+
break
286+
} else if (basename === 'animation-duration' || basename === 'transition-duration') {
287+
durations.push(stringifyNode(value))
288+
break
289+
} else if (basename === 'transition-timing-function' || basename === 'animation-timing-function') {
290+
timingFunctions.push(stringifyNode(value))
291+
break
292+
}
257293

258-
switch (fullProperty.basename) {
259-
case 'z-index': {
260-
zindex.push(value)
261-
break
262-
}
263-
case 'text-shadow': {
264-
textShadows.push(value)
265-
break
266-
}
267-
case 'box-shadow': {
268-
boxShadows.push(value)
269-
break
270-
}
271-
case 'font': {
272-
fontValues.push(value)
273-
break
274-
}
275-
case 'font-family': {
276-
fontFamilyValues.push(stringifyNode(value))
277-
// Prevent analyzer to find color names in this property
278-
return this.skip
279-
}
280-
case 'font-size': {
281-
fontSizeValues.push(stringifyNode(value))
282-
break
283-
}
284-
case 'transition':
285-
case 'animation': {
286-
animations.push(value.children)
287-
break
288-
}
289-
case 'animation-duration':
290-
case 'transition-duration': {
291-
durations.push(stringifyNode(value))
292-
break
293-
}
294-
case 'transition-timing-function':
295-
case 'animation-timing-function': {
296-
timingFunctions.push(stringifyNode(value))
297-
break
298-
}
294+
// These properties potentially contain colors
295+
if (basename === 'text-shadow') {
296+
textShadows.push(value)
297+
} else if (basename === 'box-shadow') {
298+
boxShadows.push(value)
299299
}
300300

301301
walk(value, function (valueNode) {

0 commit comments

Comments
 (0)