Skip to content

Commit e944a5b

Browse files
committed
chore: Add benchmarks for JS bindings
1 parent 356f083 commit e944a5b

6 files changed

Lines changed: 1431 additions & 34 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ into:
4141
- Works on Linux, Windows, and macOS
4242
- Supports HTML5 & CSS3
4343
- Bindings for [Python](https://github.com/Stranger6667/css-inline/tree/master/bindings/python), [Ruby](https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby), [JavaScript](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript), [C](https://github.com/Stranger6667/css-inline/tree/master/bindings/c), and a [WebAssembly](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript/wasm) module to run in browsers.
44+
- Command Line Interface
4445

4546
## Playground
4647

bindings/javascript/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ The `data-css-inline="ignore"` attribute also allows you to skip `link` and `sty
132132
</script>
133133
```
134134

135+
## Performance
136+
137+
`css-inline` is powered by efficient tooling from Mozilla's Servo project and significantly outperforms other JavaScript alternatives in terms of speed.
138+
Most of the time it achieves over a **4x** speed advantage compared to the next fastest alternative.
139+
140+
Here is the performance comparison:
141+
142+
| | Size | `css-inline 0.11.2` | `css-inline-wasm 0.11.2` | `juice 10.0.0` | `inline-css 4.0.2` |
143+
|-------------|---------|---------------------|--------------------------|-----------------------|------------------------|
144+
| Basic | 230 B | 15.19 µs | 29.33 µs (**1.93x**) | 142.53 µs (**9.38x**) | 163.77 µs (**10.78x**) |
145+
| Realistic-1 | 8.58 KB | 336.81 µs | 638.97 µs (**1.90x**) | 1.28 ms (**3.80x**) | 1.87 ms (**5.57x**) |
146+
| Realistic-2 | 4.3 KB | 210.52 µs | 385.20 µs (**1.82x**) | 1.72 ms (**8.18x**) | 1.56 ms (**7.44x**) |
147+
148+
The "Basic" case was obtained from benchmarking the example from the Usage section.
149+
150+
The benchmarking code is available in the `benches/bench.ts` file. The benchmarks were conducted using the stable `rustc 1.74.1` on Node.js `v21.4.0`.
151+
135152
## License
136153

137154
This project is licensed under the terms of the [MIT license](https://opensource.org/licenses/MIT).
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { promises as fs } from "fs";
2+
import { join } from "path";
3+
import b from "benny";
4+
import juice from "juice";
5+
import inlineCss from "inline-css";
6+
7+
import { inline } from "../index";
8+
import { initWasm, inline as wasmInline } from "../wasm";
9+
10+
async function run() {
11+
const benchmarksPath = join(__dirname, "../../../benchmarks/benchmarks.json");
12+
const benches = JSON.parse(await fs.readFile(benchmarksPath, "utf-8"));
13+
await initWasm(fs.readFile(join(__dirname, "../wasm/index_bg.wasm")));
14+
15+
for (const { name, html } of benches) {
16+
await b.suite(
17+
name,
18+
b.add("css-inline", () => {
19+
inline(html);
20+
}),
21+
b.add("css-inline-wasm", () => {
22+
wasmInline(html);
23+
}),
24+
b.add("juice", () => {
25+
juice(html);
26+
}),
27+
b.add("inline-css", () => {
28+
inlineCss(html);
29+
}),
30+
b.cycle(),
31+
b.complete(),
32+
);
33+
}
34+
}
35+
36+
run().catch((e) => {
37+
console.error(e);
38+
});

bindings/javascript/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@
3838
"@swc/core": "^1.3.101",
3939
"@types/copyfiles": "^2",
4040
"ava": "^6.0.1",
41+
"benny": "^3.7.1",
4142
"copyfiles": "^2.4.1",
4243
"dts-bundle-generator": "^9.1.0",
4344
"esbuild": "^0.19.10",
45+
"inline-css": "^4.0.2",
46+
"juice": "^10.0.0",
4447
"npm-run-all2": "^6.1.1",
4548
"typescript": "^5.3.3"
4649
},
@@ -65,6 +68,7 @@
6568
},
6669
"scripts": {
6770
"artifacts": "napi artifacts",
71+
"bench": "node -r @swc-node/register benches/bench.ts",
6872
"bundle": "run-p 'bundle:*'",
6973
"bundle:js": "node bundle.js",
7074
"bundle:dts": "dts-bundle-generator --external-types -o wasm/index.d.ts wasm-binding.ts",

0 commit comments

Comments
 (0)