Skip to content

Commit

Permalink
make Collection smaller by re-using objects and reducing arrays creat…
Browse files Browse the repository at this point in the history
…ed (#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
```
  • Loading branch information
bartveneman authored Dec 31, 2023
1 parent 1b2a505 commit e5a7ce1
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ export class Collection {

if (useLocations) {
/** @type {number[]} */
this._node_lines = []
/** @type {number[]} */
this._node_columns = []
/** @type {number[]} */
this._node_lengths = []
/** @type {number[]} */
this._node_offsets = []
this._nodes = []
}

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

this._node_lines[index] = start.line
this._node_columns[index] = start.column
this._node_offsets[index] = start_offset
this._node_lengths[index] = node_location.end.offset - start_offset
this._nodes[position] = start.line
this._nodes[position + 1] = start.column
this._nodes[position + 2] = start_offset
this._nodes[position + 3] = node_location.end.offset - start_offset
}

if (this._items.has(item)) {
Expand All @@ -60,43 +55,56 @@ export class Collection {
* @property {number} offset
* @property {number} length
*
* @returns {{ total: number; totalUnique: number; uniquenessRatio: number; unique: Record<string, number>; __unstable__uniqueWithLocations: Record<string, CssLocation[]>}}
* @returns {{
* total: number;
* totalUnique: number;
* uniquenessRatio: number;
* unique: Record<string, number>;
* } & ({
* __unstable__uniqueWithLocations: Record<string, CssLocation[]>
* } | {
* __unstable__uniqueWithLocations?: undefined
* })}
*/
c() {
let useLocations = this._useLocations
/** @type {Map<string, CssLocation[]>} */
let uniqueWithLocations = new Map()
/** @type {Record<string, number>} */
let unique = {}
let useLocations = this._useLocations
let items = this._items
let _nodes = this._nodes
let size = items.size

items.forEach((list, key) => {
if (useLocations) {
let nodes = list.map(index => ({
line: this._node_lines[index],
column: this._node_columns[index],
offset: this._node_offsets[index],
length: this._node_lengths[index],
}))
let nodes = list.map(index => {
let position = index * 4
/** @type {CssLocation} */
return {
line: _nodes[position],
column: _nodes[position + 1],
offset: _nodes[position + 2],
length: _nodes[position + 3],
}
})
uniqueWithLocations.set(key, nodes)
}
unique[key] = list.length
})

if (this._useLocations) {
return {
total: this._total,
totalUnique: size,
unique,
uniquenessRatio: this._total === 0 ? 0 : size / this._total,
__unstable__uniqueWithLocations: Object.fromEntries(uniqueWithLocations),
}
}

return {
total: this._total,
let total = this._total
let data = {
total,
totalUnique: size,
unique,
uniquenessRatio: this._total === 0 ? 0 : size / this._total,
uniquenessRatio: total === 0 ? 0 : size / total,
}

if (useLocations) {
data.__unstable__uniqueWithLocations = Object.fromEntries(uniqueWithLocations)
}

return data
}
}

0 comments on commit e5a7ce1

Please sign in to comment.