Skip to content

Commit f567bbf

Browse files
committed
Add benchmarks
1 parent 12d320e commit f567bbf

File tree

7 files changed

+20281
-140
lines changed

7 files changed

+20281
-140
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</h3>
66

77
<h6 align="center">
8-
<p><a href="#how-does-it-work">Mangle classnames in production</a> - save up to 20% on css for free!</p>
8+
<p><a href="#how-does-it-work">Mangle classnames in production</a> - save up to 30% on css and 90% of build time for free!</p>
99
</h6>
1010

1111
<p align="center">
@@ -46,6 +46,20 @@ export default defineConfig({
4646

4747
And that's it. When running `vite build` your generated CSS should be significantly smaller.
4848

49+
### Benchmarks
50+
51+
Benchmarks are done against [bootstrap](https://getbootstrap.com/docs/5.0/getting-started/introduction/) and [materialize.css](https://materializecss.com/getting-started.html) assuming all the classes are used as css modules.
52+
The benchmark code is located in the [benchmarks](./benchmarks) directory.
53+
54+
Run them by building the plugin via `npm run build` and then running `npm run benchmarks`.
55+
The results below are from a MacBook Air M2 with node v22.8.0.
56+
57+
| Input | Build Time | Gzip Size | Brotli Size |
58+
|----------------------------------------------------------------------------------|---------------------------------------|------------------------------------------|-----------------------------------------|
59+
| [bootstrap-5.0.2.module.css](benchmarks/fixtures/bootstrap-5.0.2.module.css) | 525ms (_**-94.06%**_ / _**-8311ms**_) | 21.3 kB (_**-26.53%**_ / _**-7.69 kB**_) | 21.3 kB (_**-27.54%**_ / _**-6 kB**_) |
60+
| [materialize-1.0.0.module.css](benchmarks/fixtures/materialize-1.0.0.module.css) | 572ms (_**-92.59%**_ / _**-7156ms**_) | 20.1 kB (_**-19.70%**_ / _**-4.93 kB**_) | 20.1 kB (_**-21.33%**_ / _**-4.3 kB**_) |
61+
62+
4963
### How does it work?
5064

5165
By default, when using css modules, you end up with hashes or other long class-names in your bundled css files:

benchmarks/bundler.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {isMainThread, parentPort, workerData} from 'node:worker_threads';
2+
import {join} from 'node:path';
3+
import {mkdir, writeFile, stat} from 'node:fs/promises'
4+
import {build} from 'vite'
5+
import {gzipSize} from 'gzip-size';
6+
import * as brotliSize from 'brotli-size';
7+
import {randomUUID} from 'node:crypto';
8+
import {optimizeCssModules} from '../dist/index.mjs';
9+
10+
if (isMainThread) {
11+
throw new Error('This module must be run as a worker.');
12+
}
13+
14+
const benchmark = join(process.cwd(), 'benchmarks');
15+
const output = join(benchmark, '.output', randomUUID());
16+
17+
// Clean and re-create output directory
18+
await mkdir(output).catch(() => 0);
19+
20+
const sizeFor = async (string) => ({
21+
gzip: await gzipSize(string),
22+
brotli: brotliSize.sync(string),
23+
});
24+
25+
const {input, optimize = false} = workerData;
26+
const root = join(output, `${input}.html`);
27+
const module = join(output, `${input}.ts`);
28+
const css = join(benchmark, 'fixtures', input);
29+
30+
await writeFile(root, `<script type="module" src="${module}"></script>`)
31+
await writeFile(module, `import styles from '${css}'; console.log(Object.entries(styles));`)
32+
33+
const start = process.hrtime.bigint();
34+
const result = await build({
35+
plugins: optimize ? [optimizeCssModules()] : [],
36+
build: {
37+
outDir: output,
38+
rollupOptions: {
39+
input: {
40+
app: root
41+
}
42+
}
43+
}
44+
});
45+
46+
const builtTime = process.hrtime.bigint() - start;
47+
const minifiedCssFile = result.output.find(v => v.fileName.endsWith('.css')).source;
48+
const sizes = await sizeFor(minifiedCssFile);
49+
50+
parentPort.postMessage({
51+
input,
52+
builtTime: Number(builtTime),
53+
optimized: optimize,
54+
sizes: {
55+
...sizes,
56+
original: (await stat(css)).size
57+
}
58+
});
59+
60+

0 commit comments

Comments
 (0)