Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"rules": {
"no-null": "off", // AST's need null
"prefer-const": "error",
"jest/no-conditional-in-test": "off",
"vitest/no-conditional-in-test": "off",
"jest/no-conditional-expect": "off",
Expand Down
16 changes: 8 additions & 8 deletions benchmark/comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ for (const file of files) {
})

bench.add(`Parse+Walk|${file}`, () => {
let ast = parse(css)
const ast = parse(css)
walk(ast, (node, _depth) => {
void node.type
void node.line
})
})

bench.add(`Fair-Wallace|${file}`, () => {
let ast = parse(css, {
const ast = parse(css, {
parse_selectors: false,
parse_values: false,
parse_atrule_preludes: false,
Expand All @@ -61,7 +61,7 @@ for (const file of files) {
})

bench.add(`Fair-CSSTree|${file}`, () => {
let ast = csstree.parse(css, {
const ast = csstree.parse(css, {
positions: true,
parseValue: false,
parseAtrulePrelude: false,
Expand All @@ -83,7 +83,7 @@ for (const file of files) {
})

bench.add(`CSSTree|${file}`, () => {
let ast = csstree.parse(css, { positions: true })
const ast = csstree.parse(css, { positions: true })
// @ts-expect-error: no type definitions for css-tree
csstree.walk(ast, (node) => {
void node.type
Expand All @@ -92,7 +92,7 @@ for (const file of files) {
})

bench.add(`PostCSS|${file}`, () => {
let root = postcss.parse(css)
const root = postcss.parse(css)
root.walk((node) => {
void node.type
void node.source?.start?.line
Expand Down Expand Up @@ -140,20 +140,20 @@ function measureMemoryMB(
const before = process.memoryUsage()

if (parser === 'wallace') {
let ast = parse(css)
const ast = parse(css)
walk(ast, (node, _depth) => {
void node.type
void node.line
})
} else if (parser === 'csstree') {
let ast = csstree.parse(css, { positions: true })
const ast = csstree.parse(css, { positions: true })
// @ts-expect-error: no type definitions for css-tree
csstree.walk(ast, (node) => {
void node.type
void node.loc?.start.line
})
} else {
let root = postcss.parse(css)
const root = postcss.parse(css)
root.walk((node) => {
void node.type
void node.source?.start?.line
Expand Down
16 changes: 8 additions & 8 deletions src/arena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
// lastChild field saved another 4 bytes. startColumn was changed from Uint16 to Uint32 to avoid
// overflow on long lines (common in minified CSS).

let BYTES_PER_NODE = 32
const BYTES_PER_NODE = 32

// Node type constants
export const STYLESHEET = 1
Expand Down Expand Up @@ -141,10 +141,10 @@ export class CSSDataArena {

// Calculate recommended initial capacity based on CSS source size
static capacity_for_source(source_length: number): number {
let size_in_kb = source_length / 1024
let estimated_nodes = Math.ceil(size_in_kb * CSSDataArena.NODES_PER_KB)
const size_in_kb = source_length / 1024
const estimated_nodes = Math.ceil(size_in_kb * CSSDataArena.NODES_PER_KB)
// Add 15% buffer to avoid growth during parsing
let capacity = Math.ceil(estimated_nodes * CSSDataArena.CAPACITY_BUFFER)
const capacity = Math.ceil(estimated_nodes * CSSDataArena.CAPACITY_BUFFER)
// Ensure minimum capacity of 16 nodes (even for empty stylesheets)
return Math.max(16, capacity)
}
Expand Down Expand Up @@ -313,8 +313,8 @@ export class CSSDataArena {
// Grow the arena by 1.3x when capacity is exceeded
private grow(): void {
this.growth_count++
let new_capacity = Math.ceil(this.capacity * CSSDataArena.GROWTH_FACTOR)
let new_buffer = new ArrayBuffer(new_capacity * BYTES_PER_NODE)
const new_capacity = Math.ceil(this.capacity * CSSDataArena.GROWTH_FACTOR)
const new_buffer = new ArrayBuffer(new_capacity * BYTES_PER_NODE)

// Copy existing data to new buffer
new Uint8Array(new_buffer).set(new Uint8Array(this.buffer))
Expand Down Expand Up @@ -375,8 +375,8 @@ export class CSSDataArena {
/** Shrink the buffer to the live node count, releasing wasted capacity. Call once after parsing; no-op if already tight. */
trim(): void {
if (this.count === this.capacity) return
let byte_count = this.count * BYTES_PER_NODE
let new_buffer = new ArrayBuffer(byte_count)
const byte_count = this.count * BYTES_PER_NODE
const new_buffer = new ArrayBuffer(byte_count)
new Uint8Array(new_buffer).set(new Uint8Array(this.buffer, 0, byte_count))
this.buffer = new_buffer
this.view = new DataView(new_buffer)
Expand Down
Loading
Loading