Repository navigation
Expand file tree
/
Copy pathbench.ts
More file actions
101 lines (84 loc) · 2.78 KB
/
Copy pathbench.ts
File metadata and controls
101 lines (84 loc) · 2.78 KB
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
import {
parseRangeHeader,
buildRangeResponseHeaders,
build416Headers,
isConditionalFresh,
isPreconditionFailure,
isRangeFresh,
evaluateConditionalRequest,
buildContentDisposition,
} from "./src/index";
const ITERATIONS = 2_000_000;
function bench(name: string, fn: () => void): number {
for (let i = 0; i < 50_000; i++) fn(); // warmup
const start = performance.now();
for (let i = 0; i < ITERATIONS; i++) fn();
const elapsed = performance.now() - start;
const opsPerSec = Math.round(ITERATIONS / (elapsed / 1000));
const nsPerOp = Math.round((elapsed / ITERATIONS) * 1_000_000);
console.log(` ${name.padEnd(44)} ${(opsPerSec / 1_000_000).toFixed(1).padStart(6)}M ops/sec ${String(nsPerOp).padStart(5)}ns/op`);
return opsPerSec;
}
console.log("\n=== Kernel micro-benchmarks ===\n");
const webHeaders304 = new Headers({
"if-none-match": '"abc123"',
"if-modified-since": "Sat, 28 Jun 2025 12:00:00 GMT",
});
const webHeaders206 = new Headers({
"range": "bytes=0-499",
});
const webHeadersFull = new Headers({
"range": "bytes=0-499",
"if-none-match": '"abc123"',
"if-match": '"abc123"',
"if-modified-since": "Sat, 28 Jun 2025 12:00:00 GMT",
"if-range": '"abc123"',
});
bench("parseRangeHeader", () => {
parseRangeHeader("bytes=0-499", 10000);
});
bench("buildRangeResponseHeaders", () => {
buildRangeResponseHeaders({ totalSize: 10000, range: { start: 0, end: 499 }, contentType: "video/mp4", etag: '"abc123"', lastModified: "2025-06-28T12:00:00.000Z" });
});
bench("isConditionalFresh", () => {
isConditionalFresh(webHeaders304, '"abc123"', "Sat, 28 Jun 2025 12:00:00 GMT");
});
bench("isPreconditionFailure", () => {
isPreconditionFailure(webHeadersFull, '"abc123"', "Sat, 28 Jun 2025 12:00:00 GMT");
});
bench("isRangeFresh", () => {
isRangeFresh(webHeadersFull, '"abc123"', "Sat, 28 Jun 2025 12:00:00 GMT");
});
bench("evaluateConditionalRequest (304 path)", () => {
evaluateConditionalRequest(webHeaders304, {
totalSize: 10000,
contentType: "video/mp4",
etag: '"abc123"',
lastModified: "2025-06-28T12:00:00.000Z",
});
});
bench("evaluateConditionalRequest (206 path)", () => {
evaluateConditionalRequest(webHeaders206, {
totalSize: 10000,
contentType: "video/mp4",
etag: '"abc123"',
lastModified: "2025-06-28T12:00:00.000Z",
});
});
bench("evaluateConditionalRequest (200 no cond)", () => {
evaluateConditionalRequest(new Headers(), {
totalSize: 10000,
contentType: "video/mp4",
etag: '"abc123"',
lastModified: "2025-06-28T12:00:00.000Z",
});
});
bench("buildContentDisposition (ASCII)", () => {
buildContentDisposition("report.pdf");
});
bench("buildContentDisposition (non-ASCII)", () => {
buildContentDisposition("Årlig_Rapport.pdf");
});
bench("build416Headers", () => {
build416Headers(10000);
});