-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.bench.ts
122 lines (104 loc) · 3.11 KB
/
router.bench.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { createRouter } from "./mod.ts";
import {
createRouteMap as createRenoMap,
createRouter as createReno,
forMethod,
} from "https://deno.land/x/[email protected]/reno/mod.ts";
import { Node } from "https://deno.land/x/[email protected]/mod.ts";
const router = createRouter({
"/endpoint": () => new Response("Hello"),
"/endpoint2/:id": {
POST: (_req, params) =>
Promise.resolve(
new Response(JSON.stringify(params), {
headers: { "Content-Type": "application/json" },
}),
),
},
});
const reno = createReno(createRenoMap([
["/endpoint", () => new Response("Hello")],
[
/\/endpoint2\/?(\d+\w+)/,
forMethod([
["POST", (req) =>
Promise.resolve(
new Response(JSON.stringify({ id: req.routeParams[0] }), {
headers: { "Content-Type": "application/json" },
}),
)],
]),
],
]));
const routerr = new Node();
routerr.add("/endpoint", () => new Response("Hello"));
routerr.add(
"/endpoint2/:id",
(params: unknown) =>
new Response(JSON.stringify(params), {
headers: { "Content-Type": "application/json" },
}),
);
// Minimal server implementation since router doesn't work with Request objects
const routeWithRouter = (req: Request) => {
const [h, p] = routerr.find(req.url);
if (h) {
return h(p);
} else {
return new Response("Not Found", { status: 404 });
}
};
const baseUrl = "http://localhost";
const urlParams = new URLSearchParams({ param1: "foo" });
urlParams.append("param2", "abc");
urlParams.append("param2", "cde");
const queryString = urlParams.toString();
// routeno benchmarks
// @ts-ignore unstable
Deno.bench("routeno GET", { group: "GET", baseline: true }, async () => {
await router(new Request(`${baseUrl}/endpoint`));
});
// @ts-ignore unstable
Deno.bench(
"routeno GET + params",
{ group: "GET + params", baseline: true },
async () => {
await router(new Request(`${baseUrl}/endpoint?${queryString}`));
},
);
// @ts-ignore unstable
Deno.bench("routeno POST", { group: "POST", baseline: true }, async () => {
await router(
new Request(`${baseUrl}/endpoint2/123123123`, { method: "POST" }),
);
});
// reno benchmarks
// @ts-ignore unstable
Deno.bench("reno GET", { group: "GET" }, async () => {
await reno(new Request(`${baseUrl}/endpoint`));
});
// @ts-ignore unstable
Deno.bench("reno GET + params", { group: "GET + params" }, async () => {
await reno(new Request(`${baseUrl}/endpoint?${queryString}`));
});
// @ts-ignore unstable
Deno.bench("reno POST", { group: "POST" }, async () => {
await reno(
new Request(`${baseUrl}/endpoint2/123123123`, { method: "POST" }),
);
});
// router benchmarks
// @ts-ignore unstable
Deno.bench("router GET", { group: "GET" }, () => {
routeWithRouter(new Request(`${baseUrl}/endpoint`));
});
// @ts-ignore unstable
Deno.bench("router GET + params", { group: "GET + params" }, () => {
routeWithRouter(new Request(`${baseUrl}/endpoint?${queryString}`));
});
// @ts-ignore unstable
Deno.bench("router POST", { group: "POST" }, () => {
routeWithRouter(
new Request(`${baseUrl}/endpoint2/123123123`, { method: "POST" }),
);
});