comparison benchmark/bun-http-framework-benchmark/src/bun/bun.ts @ 183:a8976a008a9d

[BenchMark] Added bun bench mark to test seoboe vs other popular benchmarks.
author MrJuneJune <me@mrjunejune.com>
date Fri, 23 Jan 2026 21:19:08 -0800
parents
children
comparison
equal deleted inserted replaced
179:8d17f6e6e290 183:a8976a008a9d
1 const jsonHeaders = { headers: [['Content-Type', 'application/json']] },
2 queryHeaders = { headers: [['X-Powered-By', 'benchmark']] },
3 notFound = new Response(null, { status: 404 }),
4 hiRes = new Response('Hi')
5
6 function toResponse(json: unknown) {
7 return new Response(JSON.stringify(json), jsonHeaders)
8 }
9
10 // json -> [106, 115, 111, 110]
11 // id/ -> [105, 100, 47]
12 // Simulate the maximum performance you can get with Bun.serve
13 // We should appreciate how frameworks make all these stuff easier :) - Reve
14 Bun.serve({
15 static: {
16 '/': new Response('Hi', {
17 headers: { 'content-type': 'text/plain;charset=utf8' }
18 })
19 },
20 routes: {
21 '/id/:id': {
22 GET(request: Request) {
23 const url = request.url,
24 s = url.indexOf('/', 11),
25 qi = url.indexOf('?', s + 1)
26
27 const nameIdx = url.indexOf('name=', qi + 1)
28 const nameEndIdx = url.indexOf('&', nameIdx + 1)
29 return new Response(
30 `${request.params.id} ${
31 nameEndIdx === -1
32 ? url.substring(nameIdx + 5)
33 : url.substring(nameIdx + 5, nameEndIdx)
34 }`,
35 queryHeaders
36 )
37 }
38 }
39 },
40 fetch(req): Response | Promise<Response> {
41 const url = req.url
42
43 const pathIndex = url.indexOf('/', 12) + 1
44 const queryIndex = url.indexOf('?', pathIndex)
45 const path =
46 queryIndex === -1
47 ? url.substring(pathIndex)
48 : url.substring(pathIndex, queryIndex)
49
50 if (path.length === 0)
51 return req.method === 'GET' ? hiRes.clone() : notFound
52
53 switch (path.charCodeAt(0)) {
54 case 105:
55 if (
56 path.charCodeAt(1) === 100 &&
57 path.charCodeAt(2) === 47 &&
58 req.method === 'GET'
59 ) {
60 // Shouldn't include a slash and should have query
61 if (queryIndex === -1 || path.indexOf('/', 3) !== -1)
62 return notFound
63
64 const nameQueryIdx = url.indexOf('name=', queryIndex + 1)
65 if (nameQueryIdx === -1) return notFound
66
67 const nameQueryEndIdx = url.indexOf('&', nameQueryIdx + 1)
68 return new Response(
69 `${path.substring(3, queryIndex)} ${
70 nameQueryEndIdx === -1
71 ? url.substring(nameQueryIdx + 5)
72 : url.substring(
73 nameQueryIdx + 5,
74 nameQueryEndIdx
75 )
76 }`,
77 queryHeaders
78 )
79 }
80
81 return notFound
82
83 case 106:
84 return path.charCodeAt(1) === 115 &&
85 path.charCodeAt(2) === 111 &&
86 path.charCodeAt(3) === 110 &&
87 req.method === 'POST'
88 ? req.json().then(toResponse)
89 : notFound
90
91 default:
92 return notFound
93 }
94 }
95 })