ReviewOS

also looking at this

stacks/ts-css

Merge @stacksjs/ts-css into this repo as packages/toolkit

#27
Merged glennmichael123 wants to merge merge/ts-css-toolkit into main
128 files +9797 -201
packages/toolkit/bench/index.bench.tsadded+185-0
Changes to packages/toolkit/bench/index.bench.ts
@@ -0,0 +1,185 @@
1/**
2 * Mitata bench: ts-css vs the original css-tree / css-select / css-what / csso
3 * libraries. Run with:
4 *
5 * bun bench/index.bench.ts
6 *
7 * Prints per-operation timings and ratios. The fixtures under
8 * `test/fixtures/` are concatenated into one ~6 KB stylesheet (small but
9 * representative of the SVGO/optimize use case).
10 */
11
12import { readFileSync } from 'node:fs'
13import { join } from 'node:path'
14import * as cssTree from 'css-tree'
15import * as cssSelect from 'css-select'
16import * as cssWhat from 'css-what'
17import * as csso from 'csso'
18import { barplot, bench, group, run, summary } from 'mitata'
19
20import * as ourCss from '../src'
21import * as ourCsso from '../src/optimize'
22import * as ourSelect from '../src/select'
23import * as ourWhat from '../src/what'
24
25const FIX = join(import.meta.dir, '..', 'test', 'fixtures')
26const samples = ['reset.css', 'utilities.css', 'component.css'].map(f => readFileSync(join(FIX, f), 'utf8'))
27const sample = samples.join('\n')
28
29console.log(`Fixture: ${sample.length} bytes (concatenation of reset+utilities+component).`)
30console.log(`Bun ${Bun.version}`)
31console.log()
32
33// ---- Parse ----
34group('parse stylesheet', () => {
35 barplot(() => {
36 bench('ts-css', () => ourCss.parse(sample))
37 bench('css-tree', () => cssTree.parse(sample))
38 })
39})
40
41// ---- Generate ----
42{
43 const ourAst = ourCss.parse(sample)
44 const treeAst = cssTree.parse(sample)
45 group('generate stylesheet', () => {
46 barplot(() => {
47 bench('ts-css', () => ourCss.generate(ourAst))
48 bench('css-tree', () => cssTree.generate(treeAst))
49 })
50 })
51}
52
53// ---- Walk ----
54{
55 const ourAst = ourCss.parse(sample)
56 const treeAst = cssTree.parse(sample)
57 group('walk all nodes', () => {
58 barplot(() => {
59 bench('ts-css', () => {
60 let n = 0
61 ourCss.walk(ourAst, () => { n++ })
62 return n
63 })
64 bench('css-tree', () => {
65 let n = 0
66 cssTree.walk(treeAst, () => { n++ })
67 return n
68 })
69 })
70 })
71}
72
73// ---- Selector parse ----
74const SELECTORS = [
75 'div',
76 '.foo',
77 '#bar',
78 'p > span.foo',
79 'a:hover[data-x="y" i]',
80 'div + .x ~ p:nth-child(2n+1):not(.disabled)',
81 '*[data-foo],[data-bar]:where(.a, .b)',
82]
83group('parse 7 selectors', () => {
84 barplot(() => {
85 bench('ts-css', () => {
86 for (const s of SELECTORS) ourWhat.parse(s)
87 })
88 bench('css-what', () => {
89 for (const s of SELECTORS) cssWhat.parse(s)
90 })
91 })
92})
93
94// ---- selectAll on a synthetic tree ----
95{
96 interface El { type: 'el', name: string, attrs: Record<string, string>, children: El[], parent: El | null }
97 function el(name: string, attrs: Record<string, string> = {}, ...children: El[]): El {
98 const e: El = { type: 'el', name, attrs, children, parent: null }
99 for (const c of children) c.parent = e
100 return e
101 }
102 // 5-level deep tree, ~250 nodes
103 const root = el('html', {},
104 el('body', { class: 'app' },
105 ...Array.from({ length: 10 }, (_, i) =>
106 el('section', { id: `s${i}`, class: i % 2 ? 'even' : 'odd' },
107 ...Array.from({ length: 4 }, (_, j) =>
108 el('article', { 'data-idx': String(j) },
109 el('h2', { class: 'title' }, el('span', { class: 'inner' })),
110 el('p', { class: i === 5 ? 'highlight' : 'normal' }, el('a', { href: '#x' })),
111 el('p', { class: 'tail' }),
112 ),
113 ),
114 ),
115 ),
116 ),
117 )
118 const adapter = {
119 isTag: (n: any): n is El => n?.type === 'el',
120 existsOne: (test: any, xs: any[]): boolean => xs.some((x: any) => adapter.isTag(x) && (test(x) || adapter.existsOne(test, x.children))),
121 getAttributeValue: (e: El, n: string) => e.attrs[n],
122 getChildren: (e: any) => e.children ?? [],
123 getName: (e: El) => e.name,
124 getParent: (e: any) => e.parent ?? null,
125 getSiblings: (e: any) => e.parent ? e.parent.children : [e],
126 getText: () => '',
127 hasAttrib: (e: El, n: string) => n in e.attrs,
128 removeSubsets: (n: any[]) => n,
129 findAll: (test: any, xs: any[]): El[] => {
130 const out: El[] = []
131 for (const x of xs) {
132 if (adapter.isTag(x)) {
133 if (test(x)) out.push(x)
134 out.push(...adapter.findAll(test, x.children))
135 }
136 }
137 return out
138 },
139 findOne: (test: any, xs: any[]): El | null => {
140 for (const x of xs) {
141 if (adapter.isTag(x)) {
142 if (test(x)) return x
143 const r = adapter.findOne(test, x.children)
144 if (r) return r
145 }
146 }
147 return null
148 },
149 }
150 const opts = { adapter, xmlMode: true } as any
151
152 const targets = ['p', 'p.highlight', 'section.odd > article > h2 .inner', '[data-idx="2"]']
153 group('selectAll on a 250-node tree', () => {
154 barplot(() => {
155 bench('ts-css', () => {
156 for (const sel of targets) ourSelect.selectAll(sel, root, opts)
157 })
158 bench('css-select', () => {
159 for (const sel of targets) cssSelect.selectAll(sel, root, opts)
160 })
161 })
162 })
163}
164
165// ---- minify ----
166group('minify stylesheet', () => {
167 barplot(() => {
168 bench('ts-css', () => ourCsso.minify(sample))
169 bench('csso', () => csso.minify(sample))
170 })
171})
172
173// ---- summary ----
174summary(() => {
175 bench('ts-css combined parse→minify→generate', () => {
176 const { css } = ourCsso.minify(sample)
177 return css.length
178 })
179 bench('original combined parse→minify→generate', () => {
180 const { css } = csso.minify(sample)
181 return css.length
182 })
183})
184
185await run()