Skip to content

Commit 6c2554b

Browse files
committed
add benchmarking and performance improvements
1 parent 8f9c600 commit 6c2554b

File tree

4 files changed

+85
-24
lines changed

4 files changed

+85
-24
lines changed

bench.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { run, bench, group, baseline } from 'mitata';
2+
import httpNext from './index'
3+
import httpPrevious from '0http-bun'
4+
5+
function setupRouter(router) {
6+
router.use((req, next) => {
7+
return next()
8+
})
9+
10+
router.get('/', () => {
11+
return new Response()
12+
})
13+
router.get('/:id', async (req) => {
14+
return new Response(req.params.id)
15+
})
16+
router.get('/:id/error', () => {
17+
throw new Error('Error')
18+
})
19+
}
20+
21+
const { router } = httpNext()
22+
setupRouter(router)
23+
24+
const { router: routerPrevious } = httpPrevious()
25+
setupRouter(routerPrevious)
26+
27+
group('Next Router', () => {
28+
baseline('Base URL', () => {
29+
router.fetch(new Request(new URL('http://localhost/')))
30+
})
31+
bench('Parameter URL', () => {
32+
router.fetch(new Request(new URL('http://localhost/0')))
33+
})
34+
bench('Not Found URL', () => {
35+
router.fetch(new Request(new URL('http://localhost/0/404')))
36+
})
37+
bench('Error URL', () => {
38+
router.fetch(new Request(new URL('http://localhost/0/error')))
39+
})
40+
})
41+
42+
group('Previous Router', () => {
43+
baseline('Base URL', () => {
44+
routerPrevious.fetch(new Request(new URL('http://localhost/')))
45+
})
46+
bench('Parameter URL', () => {
47+
routerPrevious.fetch(new Request(new URL('http://localhost/0')))
48+
})
49+
bench('Not Found URL', () => {
50+
routerPrevious.fetch(new Request(new URL('http://localhost/0/404')))
51+
})
52+
bench('Error URL', () => {
53+
routerPrevious.fetch(new Request(new URL('http://localhost/0/error')))
54+
})
55+
})
56+
57+
await run({
58+
silent: false,
59+
avg: true,
60+
json: false,
61+
colors: true,
62+
min_max: false,
63+
percentiles: false
64+
})

lib/next.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
module.exports = function next (middlewares, req, index, defaultRoute, errorHandler) {
2-
const middleware = middlewares[index]
3-
if (!middleware) {
2+
if (index >= middlewares.length) {
43
return defaultRoute(req)
54
}
65

7-
function step (err) {
8-
if (err) {
9-
return errorHandler(err, req)
10-
} else {
11-
return next(middlewares, req, index + 1, defaultRoute, errorHandler)
12-
}
13-
}
6+
const middleware = middlewares[index++]
147

158
try {
16-
return middleware(req, step)
9+
return middleware(req, (err) => {
10+
if (err) {
11+
return errorHandler(err, req)
12+
}
13+
return next(middlewares, req, index, defaultRoute, errorHandler)
14+
})
1715
} catch (err) {
1816
return errorHandler(err, req)
1917
}

lib/router/sequential.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
/* global Response */
2-
31
const Trouter = require('trouter')
42
const next = require('./../next')
53

4+
const status404 = {
5+
status: 404
6+
}
7+
const status500 = {
8+
status: 500
9+
}
10+
611
module.exports = (config = {}) => {
712
if (!config.defaultRoute) {
813
config.defaultRoute = () => {
9-
const res = new Response(null, {
10-
status: 404
11-
})
12-
13-
return res
14+
return new Response(null, status404)
1415
}
1516
}
1617
if (!config.errorHandler) {
1718
config.errorHandler = (err) => {
18-
const res = new Response(err.message, {
19-
status: 500
20-
})
21-
22-
return res
19+
return new Response(err.message, status500)
2320
}
2421
}
2522

@@ -43,8 +40,6 @@ module.exports = (config = {}) => {
4340

4441
req.path = url.pathname || '/'
4542
req.query = url.queryparams
46-
req.search = url.search
47-
req.hostname = url.hostname
4843

4944
const match = router.find(req.method, req.path)
5045
if (match.handlers.length > 0) {

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"type": "git",
1515
"url": "git+https://github.com/BackendStack21/0http-bun.git"
1616
},
17+
"devDependencies": {
18+
"0http-bun": "^1.0.0",
19+
"mitata": "^0.1.11"
20+
},
1721
"keywords": [
1822
"http",
1923
"server",

0 commit comments

Comments
 (0)