Skip to content

Commit

Permalink
perf: minify the heck out of some variables and strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Veneman committed Nov 21, 2023
1 parent 332f431 commit aaeacdc
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 130 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
"analytics",
"performance",
"styleguide",
"metrics"
"metrics",
"designsystem",
"fonts",
"colors",
"quality",
"code"
],
"dependencies": {
"@bramus/specificity": "^2.3.0",
Expand Down
14 changes: 9 additions & 5 deletions src/aggregate-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ function Mode(arr) {
let maxOccurrences = -1
let maxOccurenceCount = 0
let sum = 0
let len = arr.length

for (let i = 0; i < arr.length; i++) {
for (let i = 0; i < len; i++) {
let element = arr[i]
let updatedCount = (frequencies.get(element) || 0) + 1
frequencies.set(element, updatedCount)
Expand Down Expand Up @@ -70,7 +71,9 @@ class AggregateCollection {
}

aggregate() {
if (this._items.length === 0) {
let len = this._items.length

if (len === 0) {
return {
min: 0,
max: 0,
Expand All @@ -86,19 +89,20 @@ class AggregateCollection {
/** @type Number[] */
let sorted = this._items.slice().sort((a, b) => a - b)
let min = sorted[0]
let max = sorted[sorted.length - 1]
let max = sorted[len - 1]

let mode = Mode(sorted)
let median = Median(sorted)
let sum = this._sum

return {
min,
max,
mean: this._sum / sorted.length,
mean: sum / len,
mode,
median,
range: max - min,
sum: this._sum,
sum: sum,
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/atrules/atrules.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { strEquals, startsWith, endsWith } from '../string-utils.js'
import walk from 'css-tree/walker'
import {
Identifier,
MediaQuery,
MediaFeature,
Declaration,
} from '../css-tree-node-types.js'

/**
* Check whether node.property === property and node.value === value,
Expand All @@ -11,7 +17,7 @@ import walk from 'css-tree/walker'
*/
function isPropertyValue(node, property, value) {
return strEquals(property, node.property)
&& node.value.children.first.type === 'Identifier'
&& node.value.children.first.type === Identifier
&& strEquals(value, node.value.children.first.name)
}

Expand All @@ -24,7 +30,7 @@ export function isSupportsBrowserhack(prelude) {
let returnValue = false

walk(prelude, function (node) {
if (node.type === 'Declaration') {
if (node.type === Declaration) {
if (
isPropertyValue(node, '-webkit-appearance', 'none')
|| isPropertyValue(node, '-moz-appearance', 'meterbar')
Expand All @@ -47,9 +53,9 @@ export function isMediaBrowserhack(prelude) {
let returnValue = false

walk(prelude, function (node) {
if (node.type === 'MediaQuery'
if (node.type === MediaQuery
&& node.children.size === 1
&& node.children.first.type === 'Identifier'
&& node.children.first.type === Identifier
) {
node = node.children.first
// Note: CSSTree adds a trailing space to \\9
Expand All @@ -58,7 +64,7 @@ export function isMediaBrowserhack(prelude) {
return this.break
}
}
if (node.type === 'MediaFeature') {
if (node.type === MediaFeature) {
if (node.value !== null && node.value.unit === '\\0') {
returnValue = true
return this.break
Expand Down
21 changes: 12 additions & 9 deletions src/collection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class Collection {
constructor({ useLocations = false }) {
constructor({ _u = false }) {
/** @type {Map<string, number[]>} */
this._items = new Map()
this._total = 0
Expand All @@ -13,7 +13,7 @@ export class Collection {
this._node_offsets = []

/** @type {boolean} */
this._useLocations = useLocations
this._useLocations = _u
}

/**
Expand Down Expand Up @@ -54,16 +54,19 @@ export class Collection {
* @returns {{ total: number; totalUnique: number; uniquenessRatio: number; unique: Record<string, number>; __unstable__uniqueWithLocations: Record<string, CssLocation[]>}}
*/
count() {
let useLocations = this._useLocations
let uniqueWithLocations = new Map()
let unique = {}
this._items.forEach((list, key) => {
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],
}))
uniqueWithLocations.set(key, nodes)
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],
}))
uniqueWithLocations.set(key, nodes)
}
unique[key] = list.length
})

Expand Down
6 changes: 3 additions & 3 deletions src/context-collection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Collection } from './collection.js'

class ContextCollection {
constructor({ useLocations = false }) {
this._list = new Collection({ useLocations })
constructor({ _u = false }) {
this._list = new Collection({ _u })
/** @type {Map<string, Collection>} */
this._contexts = new Map()
/** @type {boolean} */
this._useLocations = useLocations
this._useLocations = _u
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/css-tree-node-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Atrule
export const Atrule = 'Atrule'
export const MediaQuery = 'MediaQuery'
export const MediaFeature = 'MediaFeature'
// Rule
export const Rule = 'Rule'

// Selector
export const Selector = 'Selector'
export const TypeSelector = 'TypeSelector'
export const PseudoClassSelector = 'PseudoClassSelector'
export const AttributeSelector = 'AttributeSelector'
export const IdSelector = 'IdSelector'
export const ClassSelector = 'ClassSelector'
export const PseudoElementSelector = 'PseudoElementSelector'

// Declaration
export const Declaration = 'Declaration'

// Values
export const Value = 'Value'
export const Identifier = 'Identifier'
export const Nth = 'Nth'
export const Combinator = 'Combinator'
export const Nr = 'Number'
export const Dimension = 'Dimension'
export const Operator = 'Operator'
export const Hash = 'Hash'
export const Url = 'Url'
export const Func = 'Function'
Loading

0 comments on commit aaeacdc

Please sign in to comment.