Skip to content

Commit

Permalink
switch from prettier/eslint to biome, use node: protocol, add bench…
Browse files Browse the repository at this point in the history
…mark
  • Loading branch information
talentlessguy committed Jul 3, 2024
1 parent 0d58fa4 commit d197307
Show file tree
Hide file tree
Showing 18 changed files with 1,719 additions and 889 deletions.
7 changes: 0 additions & 7 deletions .eslintrc

This file was deleted.

8 changes: 0 additions & 8 deletions .prettierrc.json

This file was deleted.

16 changes: 16 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"biome.enabled": true,
"editor.defaultFormatter": "biomejs.biome",
"prettier.enable": false,
"eslint.enable": false,
"editor.codeActionsOnSave": {
"source.fixAll": "always"
},
"typescript.tsdk": "node_modules/typescript/lib",
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ Check out [deno-libs/parsec](https://github.com/deno-libs/parsec) for Deno port.

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

## Install

Expand Down
15 changes: 15 additions & 0 deletions bench/body-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-check

import bodyParser from 'body-parser'
import { createServer } from 'node:http'

const mw = bodyParser.json()

const server = createServer((req, res) => {
mw(req, res, () => {
// @ts-expect-error added by body parser
res.end(JSON.stringify(req.body))
})
})

server.listen(3002)
65 changes: 65 additions & 0 deletions bench/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Benchmark

Below is a comparison of body-parser and milliparsec in terms of parsing a request with JSON payload.

### Environment

- Node.js 22.3.0
- System: Linux 6.9.7
- Machine: Asus ROG Zephyrus G16

### Benchmark command:

```sh
autocannon -b '{"a":1}' -H "Content-Type=application/json" localhost:3002 # or 3003
```

### Results

body-parser result:

```
┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬────────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼────────┤
│ Latency │ 0 ms │ 0 ms │ 0 ms │ 0 ms │ 0.01 ms │ 0.79 ms │ 251 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬───────────┬──────────┬─────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
│ Req/Sec │ 31,231 │ 31,231 │ 42,815 │ 43,935 │ 41,823.28 │ 3,470.88 │ 31,224 │
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
│ Bytes/Sec │ 4.03 MB │ 4.03 MB │ 5.52 MB │ 5.67 MB │ 5.39 MB │ 448 kB │ 4.03 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴───────────┴──────────┴─────────┘
Req/Bytes counts sampled once per second.
# of samples: 11
460k requests in 11.02s, 59.3 MB read
```

milliparsec result:

```
┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬────────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼────────┤
│ Latency │ 0 ms │ 0 ms │ 0 ms │ 0 ms │ 0.01 ms │ 0.65 ms │ 254 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬───────────┬──────────┬────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼────────┤
│ Req/Sec │ 52,511 │ 52,511 │ 63,007 │ 67,455 │ 63,397.82 │ 4,255.42 │ 52,480 │
├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼────────┤
│ Bytes/Sec │ 6.41 MB │ 6.41 MB │ 7.69 MB │ 8.23 MB │ 7.74 MB │ 519 kB │ 6.4 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴───────────┴──────────┴────────┘
Req/Bytes counts sampled once per second.
# of samples: 11
697k requests in 11.02s, 85.1 MB rea
```

## Verdict

milliparsec, on average, is ~34% faster.
15 changes: 15 additions & 0 deletions bench/milliparsec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-check

import { createServer } from 'node:http'
import * as bodyParser from '../dist/index.js'

const mw = bodyParser.json()

const server = createServer((req, res) => {
mw(req, res, () => {
// @ts-expect-error added by body parser
res.end(JSON.stringify(req.body))
})
})

server.listen(3003)
18 changes: 18 additions & 0 deletions bench/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "bench",
"type": "module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"bench": "autocannon -b '{\"a\":1}' -H \"Content-Type=application/json\""
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/body-parser": "^1.19.5",
"autocannon": "^7.15.0",
"body-parser": "^1.20.2"
}
}
Loading

0 comments on commit d197307

Please sign in to comment.