Skip to content

Commit c47434e

Browse files
author
Bart Veneman
committed
Simplify AggregateCollection and fix UInt8 bug
1 parent a199371 commit c47434e

File tree

8 files changed

+61
-70
lines changed

8 files changed

+61
-70
lines changed

benchmark/readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
`node benchmark/benchmark.js`
66

77
```
8-
Bol.com (468 kB): 8.88 ops/sec
9-
Bootstrap 5.0.0 (195 kB): 21.81 ops/sec
10-
CSS-Tricks (195 kB): 21.14 ops/sec
11-
Facebook.com (268 kB): 13.28 ops/sec
12-
GitHub.com (514 kB): 10.28 ops/sec
8+
Bol.com (468 kB): 9.19 ops/sec
9+
Bootstrap 5.0.0 (195 kB): 21.28 ops/sec
10+
CSS-Tricks (195 kB): 20.07 ops/sec
11+
Facebook.com (268 kB): 13.70 ops/sec
12+
GitHub.com (514 kB): 10.29 ops/sec
1313
Gazelle.nl (972 kB): 3.14 ops/sec
14-
Lego.com (246 kB): 16.51 ops/sec
15-
Smashing Magazine.com (1.1 MB): 3.01 ops/sec
16-
Trello.com (312 kB): 12.16 ops/sec
14+
Lego.com (246 kB): 17.43 ops/sec
15+
Smashing Magazine.com (1.1 MB): 3.29 ops/sec
16+
Trello.com (312 kB): 12.23 ops/sec
1717
```
1818

1919
## Parsing vs. analyzing

src/__fixtures__/bol-com-20190617.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@
206206
},
207207
"selectors": {
208208
"min": 1,
209-
"max": 160,
209+
"max": 640,
210210
"mean": 2.1419753086419755,
211211
"mode": 1,
212212
"median": 1,
213-
"range": 159,
213+
"range": 639,
214214
"sum": 7634,
215215
"items": [
216216
1,
@@ -876,7 +876,7 @@
876876
5,
877877
5,
878878
5,
879-
128,
879+
640,
880880
2,
881881
42,
882882
5,

src/__fixtures__/github-20210501.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,11 +2650,11 @@
26502650
},
26512651
"declarations": {
26522652
"min": 1,
2653-
"max": 250,
2653+
"max": 762,
26542654
"mean": 4.317063492063492,
26552655
"mode": 1,
26562656
"median": 1,
2657-
"range": 249,
2657+
"range": 761,
26582658
"sum": 10879,
26592659
"items": [
26602660
5,
@@ -2699,15 +2699,15 @@
26992699
1,
27002700
2,
27012701
2,
2702-
250,
2703-
250,
2704-
250,
2705-
250,
2706-
250,
2707-
250,
2708-
250,
2709-
250,
2710-
250,
2702+
762,
2703+
762,
2704+
762,
2705+
762,
2706+
762,
2707+
762,
2708+
762,
2709+
762,
2710+
762,
27112711
2,
27122712
1,
27132713
1,

src/aggregate-collection.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,21 @@ function Median(arr) {
5050
}
5151

5252
class AggregateCollection {
53-
constructor(size) {
53+
constructor() {
5454
/** @type number[] */
55-
this.items = new Uint8Array(size)
56-
this.sum = 0
57-
this.cursor = 0
55+
this.items = []
5856
}
5957

6058
/**
6159
* Add a new Integer at the end of this AggregateCollection
6260
* @param {number} item - The item to add
6361
*/
6462
add(item) {
65-
this.items[this.cursor] = item
66-
this.sum += item
67-
this.cursor++
63+
this.items.push(item)
6864
}
6965

7066
aggregate() {
71-
if (this.cursor === 0) {
67+
if (this.items.length === 0) {
7268
return {
7369
min: 0,
7470
max: 0,
@@ -81,33 +77,30 @@ class AggregateCollection {
8177
}
8278

8379
/** @type Number[] */
84-
const sorted = new Uint8Array(
85-
this.items.slice(0, this.cursor)
86-
).sort((a, b) => a - b)
80+
const sorted = this.items.slice().sort((a, b) => a - b)
8781
const min = sorted[0]
8882
const max = sorted[sorted.length - 1]
8983

84+
const sum = this.items.reduce((total, num) => (total += num))
9085
const mode = Mode(sorted)
9186
const median = Median(sorted)
9287

9388
return {
9489
min,
9590
max,
96-
mean: this.sum / this.cursor,
91+
mean: sum / this.items.length,
9792
mode,
9893
median,
9994
range: max - min,
100-
sum: this.sum,
95+
sum,
10196
}
10297
}
10398

10499
/**
105100
* @returns {number[]} All items in this collection
106101
*/
107102
toArray() {
108-
return Array.from(
109-
this.items.subarray(0, this.cursor)
110-
)
103+
return this.items
111104
}
112105
}
113106

src/aggregate-collection.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AggregateCollection } from './aggregate-collection.js'
55
const CollectionSuite = suite('AggregateCollection')
66

77
CollectionSuite('aggregates correctly', () => {
8-
const fixture = new AggregateCollection(6)
8+
const fixture = new AggregateCollection()
99
fixture.add(1)
1010
fixture.add(2)
1111
fixture.add(25)
@@ -28,7 +28,7 @@ CollectionSuite('aggregates correctly', () => {
2828
})
2929

3030
CollectionSuite('handles collections without values', () => {
31-
const fixture = new AggregateCollection(0)
31+
const fixture = new AggregateCollection()
3232
const aggregate = fixture.aggregate()
3333
const items = fixture.toArray()
3434

src/index.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const analyze = (css) => {
115115
isKeyframeSelector: this.atrule && this.atrule.name.endsWith('keyframes')
116116
})
117117

118-
// Avoid further walking of selectors to not mess with
118+
// Avoid deeper walking of selectors to not mess with
119119
// our specificity calculations in case of a selector
120120
// with :where() or :is() that contain SelectorLists
121121
// as children
@@ -193,34 +193,32 @@ const analyze = (css) => {
193193
}
194194
}
195195

196-
walk(value, {
197-
enter: function (valueNode) {
198-
switch (valueNode.type) {
199-
case 'Hash': {
200-
colors.push(stringifyNode(valueNode), property)
196+
walk(value, function (valueNode) {
197+
switch (valueNode.type) {
198+
case 'Hash': {
199+
colors.push(stringifyNode(valueNode), property)
201200

201+
return this.skip
202+
}
203+
case 'Identifier': {
204+
const { name } = valueNode
205+
// Bail out if it can't be a color name
206+
// 20 === 'lightgoldenrodyellow'.length
207+
// 3 === 'red'.length
208+
if (name.length > 20 || name.length < 3) {
202209
return this.skip
203210
}
204-
case 'Identifier': {
205-
const { name } = valueNode
206-
// Bail out if it can't be a color name
207-
// 20 === 'lightgoldenrodyellow'.length
208-
// 3 === 'red'.length
209-
if (name.length > 20 || name.length < 3) {
210-
return this.skip
211-
}
212-
if (colorNames[name.toLowerCase()]) {
213-
colors.push(stringifyNode(valueNode), property)
214-
}
215-
return this.skip
211+
if (colorNames[name.toLowerCase()]) {
212+
colors.push(stringifyNode(valueNode), property)
216213
}
217-
case 'Function': {
218-
if (colorFunctions[valueNode.name.toLowerCase()]) {
219-
colors.push(stringifyNode(valueNode), property)
220-
}
221-
// No this.skip here intentionally,
222-
// otherwise we'll miss colors in linear-gradient() etc.
214+
return this.skip
215+
}
216+
case 'Function': {
217+
if (colorFunctions[valueNode.name.toLowerCase()]) {
218+
colors.push(stringifyNode(valueNode), property)
223219
}
220+
// No this.skip here intentionally,
221+
// otherwise we'll miss colors in linear-gradient() etc.
224222
}
225223
}
226224
})

src/rules/rules.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { AggregateCollection } from '../aggregate-collection.js'
44
const analyzeRules = ({ rules }) => {
55
/** @type number */
66
const totalRules = rules.length
7-
const selectorsPerRule = new AggregateCollection(totalRules)
8-
const declarationsPerRule = new AggregateCollection(totalRules)
7+
const selectorsPerRule = new AggregateCollection()
8+
const declarationsPerRule = new AggregateCollection()
99

1010
let emptyRules = 0
1111

src/selectors/selectors.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const analyzeSelectors = ({ stringifyNode, selectors }) => {
1212
let maxSpecificity
1313
/** @type [number,number,number] */
1414
let minSpecificity
15-
let specificityA = new AggregateCollection(totalSelectors)
16-
let specificityB = new AggregateCollection(totalSelectors)
17-
let specificityC = new AggregateCollection(totalSelectors)
15+
let specificityA = new AggregateCollection()
16+
let specificityB = new AggregateCollection()
17+
let specificityC = new AggregateCollection()
1818
let totalUnique = 0
19-
const complexityAggregator = new AggregateCollection(totalSelectors);
19+
const complexityAggregator = new AggregateCollection()
2020

2121
/** @type [number,number,number][] */
2222
const specificities = []

0 commit comments

Comments
 (0)