-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_bench.ts
77 lines (66 loc) · 1.88 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
import { MethodRouter as $, URLRouter } from "./routers.ts";
import {
createRouteMap as createRenoMap,
createRouter as createReno,
forMethod,
} from "https://deno.land/x/[email protected]/reno/mod.ts";
const router = URLRouter({
"/endpoint": () => new Response("Hello"),
"/endpoint2/:id": (req, { params }) =>
$({
POST: (_) =>
Promise.resolve(
new Response(JSON.stringify(params), {
headers: { "Content-Type": "application/json" },
}),
),
})(req),
});
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" },
}),
)],
]),
],
]));
Deno.bench("GET http_router", { group: "get" }, async () => {
await router(new Request("http://localhost/endpoint"));
});
Deno.bench("GET reno", { group: "get" }, async () => {
await reno(new Request("http://localhost/endpoint"));
});
Deno.bench("POST http_router", { group: "post" }, async () => {
await router(
new Request("http://localhost/endpoint2/123123123", { method: "POST" }),
);
});
Deno.bench("POST reno", { group: "post" }, async () => {
await reno(
new Request("http://localhost/endpoint2/123123123", { method: "POST" }),
);
});
const cachedRouter = URLRouter({
"/": () => new Response(),
});
const req = new Request("http://localhost");
Deno.bench("Enable cache matching", { group: "cache" }, async () => {
await cachedRouter(req);
});
import * as prev from "https://deno.land/x/[email protected]/mod.ts";
const nonCachedRouter = prev.createRouter({
"/": () => new Response(),
});
Deno.bench(
"Disable cache matching",
{ group: "cache" },
async () => {
await nonCachedRouter(req);
},
);