Skip to content

Commit d197307

Browse files
committed
switch from prettier/eslint to biome, use node: protocol, add benchmark
1 parent 0d58fa4 commit d197307

18 files changed

+1719
-889
lines changed

.eslintrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

.prettierrc.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"biome.enabled": true,
3+
"editor.defaultFormatter": "biomejs.biome",
4+
"prettier.enable": false,
5+
"eslint.enable": false,
6+
"editor.codeActionsOnSave": {
7+
"source.fixAll": "always"
8+
},
9+
"typescript.tsdk": "node_modules/typescript/lib",
10+
"[typescript]": {
11+
"editor.defaultFormatter": "biomejs.biome"
12+
},
13+
"[javascript]": {
14+
"editor.defaultFormatter": "biomejs.biome"
15+
}
16+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ Check out [deno-libs/parsec](https://github.com/deno-libs/parsec) for Deno port.
1717

1818
- ⏩ built with `async` / `await`
1919
- 🛠 JSON / raw / urlencoded data support
20-
- 📦 tiny package size (728B)
20+
- 📦 tiny package size (675B)
2121
- 🔥 no dependencies
22-
-[tinyhttp](https://github.com/talentlessguy/tinyhttp) and Express support
22+
-[tinyhttp](https://github.com/tinyhttp/tinyhttp) and Express support
23+
- ⚡ 30% faster than body-parser
2324

2425
## Install
2526

bench/body-parser.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @ts-check
2+
3+
import bodyParser from 'body-parser'
4+
import { createServer } from 'node:http'
5+
6+
const mw = bodyParser.json()
7+
8+
const server = createServer((req, res) => {
9+
mw(req, res, () => {
10+
// @ts-expect-error added by body parser
11+
res.end(JSON.stringify(req.body))
12+
})
13+
})
14+
15+
server.listen(3002)

bench/index.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## Benchmark
2+
3+
Below is a comparison of body-parser and milliparsec in terms of parsing a request with JSON payload.
4+
5+
### Environment
6+
7+
- Node.js 22.3.0
8+
- System: Linux 6.9.7
9+
- Machine: Asus ROG Zephyrus G16
10+
11+
### Benchmark command:
12+
13+
```sh
14+
autocannon -b '{"a":1}' -H "Content-Type=application/json" localhost:3002 # or 3003
15+
```
16+
17+
### Results
18+
19+
body-parser result:
20+
21+
```
22+
┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬────────┐
23+
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
24+
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼────────┤
25+
│ Latency │ 0 ms │ 0 ms │ 0 ms │ 0 ms │ 0.01 ms │ 0.79 ms │ 251 ms │
26+
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴────────┘
27+
┌───────────┬─────────┬─────────┬─────────┬─────────┬───────────┬──────────┬─────────┐
28+
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
29+
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
30+
│ Req/Sec │ 31,231 │ 31,231 │ 42,815 │ 43,935 │ 41,823.28 │ 3,470.88 │ 31,224 │
31+
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
32+
│ Bytes/Sec │ 4.03 MB │ 4.03 MB │ 5.52 MB │ 5.67 MB │ 5.39 MB │ 448 kB │ 4.03 MB │
33+
└───────────┴─────────┴─────────┴─────────┴─────────┴───────────┴──────────┴─────────┘
34+
35+
Req/Bytes counts sampled once per second.
36+
# of samples: 11
37+
38+
460k requests in 11.02s, 59.3 MB read
39+
```
40+
41+
milliparsec result:
42+
43+
```
44+
┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬────────┐
45+
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
46+
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼────────┤
47+
│ Latency │ 0 ms │ 0 ms │ 0 ms │ 0 ms │ 0.01 ms │ 0.65 ms │ 254 ms │
48+
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴────────┘
49+
┌───────────┬─────────┬─────────┬─────────┬─────────┬───────────┬──────────┬────────┐
50+
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
51+
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼────────┤
52+
│ Req/Sec │ 52,511 │ 52,511 │ 63,007 │ 67,455 │ 63,397.82 │ 4,255.42 │ 52,480 │
53+
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼────────┤
54+
│ Bytes/Sec │ 6.41 MB │ 6.41 MB │ 7.69 MB │ 8.23 MB │ 7.74 MB │ 519 kB │ 6.4 MB │
55+
└───────────┴─────────┴─────────┴─────────┴─────────┴───────────┴──────────┴────────┘
56+
57+
Req/Bytes counts sampled once per second.
58+
# of samples: 11
59+
60+
697k requests in 11.02s, 85.1 MB rea
61+
```
62+
63+
## Verdict
64+
65+
milliparsec, on average, is ~34% faster.

bench/milliparsec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @ts-check
2+
3+
import { createServer } from 'node:http'
4+
import * as bodyParser from '../dist/index.js'
5+
6+
const mw = bodyParser.json()
7+
8+
const server = createServer((req, res) => {
9+
mw(req, res, () => {
10+
// @ts-expect-error added by body parser
11+
res.end(JSON.stringify(req.body))
12+
})
13+
})
14+
15+
server.listen(3003)

bench/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "bench",
3+
"type": "module",
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"bench": "autocannon -b '{\"a\":1}' -H \"Content-Type=application/json\""
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"@types/body-parser": "^1.19.5",
15+
"autocannon": "^7.15.0",
16+
"body-parser": "^1.20.2"
17+
}
18+
}

0 commit comments

Comments
 (0)