-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.ts
113 lines (101 loc) · 2.36 KB
/
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
// deno run --allow-write --allow-read --allow-hrtime ./bench.ts update-readme
import {
bench,
runBenchmarks,
} from "https://deno.land/std@0.122.0/testing/bench.ts";
import {
defaultColumns,
prettyBenchmarkDown,
prettyBenchmarkProgress,
prettyBenchmarkResult,
} from "https://deno.land/x/pretty_benching@v0.3.3/mod.ts";
import uri from "./uri.ts";
import jspmUrlcat from "https://jspm.dev/urlcat";
const urlcat = (jspmUrlcat as any).default;
const API_URL = "https://api.example.com/";
const id = "112233445566778899";
const limit = 5;
const offset = 5;
const RUNS = 2000;
bench({
name: "mouri",
runs: RUNS,
func(b): void {
b.start();
uri`${API_URL}/users/${id}/posts/${{
limit: limit.toString(),
offset: offset.toString(),
}}`;
b.stop();
},
});
bench({
name: "urlcat",
runs: RUNS,
func(b): void {
b.start();
urlcat(API_URL, "/users/:id/posts", { id, limit, offset });
b.stop();
},
});
bench({
name: "handwritten",
runs: RUNS,
func(b): void {
b.start();
const escapedId = encodeURIComponent(id);
const path = `/users/${escapedId}`;
const url = new URL(path, API_URL);
url.search = new URLSearchParams({
limit: limit.toString(),
offset: offset.toString(),
}).toString();
url.href;
b.stop();
},
});
runBenchmarks(
{ silent: true },
prettyBenchmarkProgress({}),
)
.then(
prettyBenchmarkResult({
parts: {
extraMetrics: true,
},
}),
)
.then(
prettyBenchmarkDown(
(markdown) => {
if (Deno.args[0] === "update-readme") {
const readme = Deno.readTextFileSync("./README.md");
const modifiedReadme = readme.replace(
/(<!-- BENCHMARKS START -->)[^]+(<!-- BENCHMARKS END -->)/g,
`$1\n${markdown.trim()}\n$2`,
);
Deno.writeTextFileSync("./README.md", modifiedReadme);
}
},
{
groups: [
{
include: /.+/,
name: "Simple URL joining",
description: `
pattern:
> \`{API_URL}/users/{id}/posts/limit={limit}&offset={offset}\`
expected result:
> \`https://api.example.com/users/112233445566778899/posts/limit=10&offset=5\`
`,
columns: [
...defaultColumns(),
],
},
],
},
),
)
.catch((e: any) => {
console.error(e.stack);
});