Skip to content

Commit c738373

Browse files
committed
feat(bench): add Express and Fastify competitors
1 parent f7b325c commit c738373

6 files changed

Lines changed: 371 additions & 0 deletions

File tree

bench/routing/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.tmp/
2+
node_modules/

bench/routing/bun.lock

Lines changed: 278 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bench/routing/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@stacksjs/routing-benchmarks",
3+
"private": true,
4+
"type": "module",
5+
"dependencies": {
6+
"elysia": "1.4.30",
7+
"express": "5.2.1",
8+
"fastify": "5.12.3",
9+
"hono": "4.13.5"
10+
}
11+
}

bench/routing/servers/express.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import process from 'node:process'
2+
3+
const port = Number(process.env.BENCH_PORT ?? 3999)
4+
const withDb = process.env.BENCH_DB === '1'
5+
6+
let express: any
7+
try {
8+
;({ default: express } = await import('express') as any)
9+
}
10+
catch {
11+
console.error('[bench] express is not installed, run `bun install --cwd bench/routing` to include it')
12+
process.exit(78)
13+
}
14+
15+
const app = express()
16+
app.use(express.json())
17+
app.get('/bench/json', (_req: any, res: any) => res.json({ hello: 'world' }))
18+
app.get('/bench/users/:id', (req: any, res: any) => res.json({ id: req.params.id }))
19+
app.post('/bench/echo', (req: any, res: any) => {
20+
const { name, count } = req.body ?? {}
21+
if (typeof name !== 'string' || typeof count !== 'number') return res.status(422).json({ errors: {} })
22+
return res.json({ name, count })
23+
})
24+
25+
if (withDb) {
26+
const { Database } = await import('bun:sqlite')
27+
const db = new Database(process.env.BENCH_DB_FILE!, { readonly: true })
28+
const selectItem = db.prepare('SELECT id, name FROM bench_items WHERE id = 1')
29+
app.get('/bench/db', (_req: any, res: any) => res.json(selectItem.get()))
30+
}
31+
32+
app.listen(port, '127.0.0.1')
33+
console.error(`[bench] express listening on ${port}`)

bench/routing/servers/fastify.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import process from 'node:process'
2+
3+
const port = Number(process.env.BENCH_PORT ?? 3999)
4+
const withDb = process.env.BENCH_DB === '1'
5+
6+
let fastify: any
7+
try {
8+
;({ default: fastify } = await import('fastify') as any)
9+
}
10+
catch {
11+
console.error('[bench] fastify is not installed, run `bun install --cwd bench/routing` to include it')
12+
process.exit(78)
13+
}
14+
15+
const app = fastify({ logger: false })
16+
app.get('/bench/json', () => ({ hello: 'world' }))
17+
app.get('/bench/users/:id', (request: any) => ({ id: request.params.id }))
18+
app.post('/bench/echo', {
19+
schema: {
20+
body: {
21+
type: 'object',
22+
required: ['name', 'count'],
23+
properties: { name: { type: 'string' }, count: { type: 'number' } },
24+
},
25+
},
26+
}, (request: any) => ({ name: request.body.name, count: request.body.count }))
27+
28+
if (withDb) {
29+
const { Database } = await import('bun:sqlite')
30+
const db = new Database(process.env.BENCH_DB_FILE!, { readonly: true })
31+
const selectItem = db.prepare('SELECT id, name FROM bench_items WHERE id = 1')
32+
app.get('/bench/db', () => selectItem.get())
33+
}
34+
35+
await app.listen({ port, host: '127.0.0.1' })
36+
console.error(`[bench] fastify listening on ${port}`)

bench/routing/targets.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ export const TARGETS: readonly Target[] = [
4444
server: 'elysia.ts',
4545
optional: true,
4646
},
47+
{
48+
id: 'express',
49+
label: 'Express',
50+
server: 'express.ts',
51+
optional: true,
52+
},
53+
{
54+
id: 'fastify',
55+
label: 'Fastify',
56+
server: 'fastify.ts',
57+
optional: true,
58+
},
4759
{
4860
id: 'hono',
4961
label: 'Hono',

0 commit comments

Comments
 (0)