Skip to content

Commit e5a7ce1

Browse files
authored
make Collection smaller by re-using objects and reducing arrays created (#380)
``` # before -rw-r--r--@ 1 bart.veneman staff 18944 Dec 31 09:26 analyzer.cjs -rw-r--r--@ 1 bart.veneman staff 90566 Dec 31 09:26 analyzer.cjs.map -rw-r--r--@ 1 bart.veneman staff 17330 Dec 31 09:26 analyzer.modern.js -rw-r--r--@ 1 bart.veneman staff 90112 Dec 31 09:26 analyzer.modern.js.map -rw-r--r--@ 1 bart.veneman staff 18718 Dec 31 09:26 analyzer.module.js -rw-r--r--@ 1 bart.veneman staff 90606 Dec 31 09:26 analyzer.module.js.map -rw-r--r--@ 1 bart.veneman staff 18899 Dec 31 09:26 analyzer.umd.js -rw-r--r--@ 1 bart.veneman staff 90767 Dec 31 09:26 analyzer.umd.js.map -rw-r--r--@ 1 bart.veneman staff 23587 Dec 31 09:26 index.d.ts # after -rw-r--r--@ 1 bart.veneman staff 18862 Dec 31 14:41 analyzer.cjs -rw-r--r--@ 1 bart.veneman staff 90460 Dec 31 14:41 analyzer.cjs.map -rw-r--r--@ 1 bart.veneman staff 17249 Dec 31 14:41 analyzer.modern.js -rw-r--r--@ 1 bart.veneman staff 90028 Dec 31 14:41 analyzer.modern.js.map -rw-r--r--@ 1 bart.veneman staff 18636 Dec 31 14:41 analyzer.module.js -rw-r--r--@ 1 bart.veneman staff 90500 Dec 31 14:41 analyzer.module.js.map -rw-r--r--@ 1 bart.veneman staff 18817 Dec 31 14:41 analyzer.umd.js -rw-r--r--@ 1 bart.veneman staff 90661 Dec 31 14:41 analyzer.umd.js.map -rw-r--r--@ 1 bart.veneman staff 27391 Dec 31 14:41 index.d.ts ```
1 parent 1b2a505 commit e5a7ce1

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

src/collection.js

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ export class Collection {
77

88
if (useLocations) {
99
/** @type {number[]} */
10-
this._node_lines = []
11-
/** @type {number[]} */
12-
this._node_columns = []
13-
/** @type {number[]} */
14-
this._node_lengths = []
15-
/** @type {number[]} */
16-
this._node_offsets = []
10+
this._nodes = []
1711
}
1812

1913
/** @type {boolean} */
@@ -30,11 +24,12 @@ export class Collection {
3024
if (this._useLocations) {
3125
let start = node_location.start
3226
let start_offset = start.offset
27+
let position = index * 4
3328

34-
this._node_lines[index] = start.line
35-
this._node_columns[index] = start.column
36-
this._node_offsets[index] = start_offset
37-
this._node_lengths[index] = node_location.end.offset - start_offset
29+
this._nodes[position] = start.line
30+
this._nodes[position + 1] = start.column
31+
this._nodes[position + 2] = start_offset
32+
this._nodes[position + 3] = node_location.end.offset - start_offset
3833
}
3934

4035
if (this._items.has(item)) {
@@ -60,43 +55,56 @@ export class Collection {
6055
* @property {number} offset
6156
* @property {number} length
6257
*
63-
* @returns {{ total: number; totalUnique: number; uniquenessRatio: number; unique: Record<string, number>; __unstable__uniqueWithLocations: Record<string, CssLocation[]>}}
58+
* @returns {{
59+
* total: number;
60+
* totalUnique: number;
61+
* uniquenessRatio: number;
62+
* unique: Record<string, number>;
63+
* } & ({
64+
* __unstable__uniqueWithLocations: Record<string, CssLocation[]>
65+
* } | {
66+
* __unstable__uniqueWithLocations?: undefined
67+
* })}
6468
*/
6569
c() {
66-
let useLocations = this._useLocations
70+
/** @type {Map<string, CssLocation[]>} */
6771
let uniqueWithLocations = new Map()
72+
/** @type {Record<string, number>} */
6873
let unique = {}
74+
let useLocations = this._useLocations
6975
let items = this._items
76+
let _nodes = this._nodes
7077
let size = items.size
7178

7279
items.forEach((list, key) => {
7380
if (useLocations) {
74-
let nodes = list.map(index => ({
75-
line: this._node_lines[index],
76-
column: this._node_columns[index],
77-
offset: this._node_offsets[index],
78-
length: this._node_lengths[index],
79-
}))
81+
let nodes = list.map(index => {
82+
let position = index * 4
83+
/** @type {CssLocation} */
84+
return {
85+
line: _nodes[position],
86+
column: _nodes[position + 1],
87+
offset: _nodes[position + 2],
88+
length: _nodes[position + 3],
89+
}
90+
})
8091
uniqueWithLocations.set(key, nodes)
8192
}
8293
unique[key] = list.length
8394
})
8495

85-
if (this._useLocations) {
86-
return {
87-
total: this._total,
88-
totalUnique: size,
89-
unique,
90-
uniquenessRatio: this._total === 0 ? 0 : size / this._total,
91-
__unstable__uniqueWithLocations: Object.fromEntries(uniqueWithLocations),
92-
}
93-
}
94-
95-
return {
96-
total: this._total,
96+
let total = this._total
97+
let data = {
98+
total,
9799
totalUnique: size,
98100
unique,
99-
uniquenessRatio: this._total === 0 ? 0 : size / this._total,
101+
uniquenessRatio: total === 0 ? 0 : size / total,
102+
}
103+
104+
if (useLocations) {
105+
data.__unstable__uniqueWithLocations = Object.fromEntries(uniqueWithLocations)
100106
}
107+
108+
return data
101109
}
102110
}

0 commit comments

Comments
 (0)