-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.ts
68 lines (60 loc) · 1.64 KB
/
loader.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
import { access, readFile } from "fs/promises";
import { fileURLToPath } from "url";
import { defineSuite, Profiler } from "esbench";
import { names } from "../src/compiler.ts";
import { load, resolve, typeCache } from "../src/loader.ts";
import { tsconfckCache } from "../src/tsconfig.ts";
import { getFilesToTransform, setCompiler } from "./helper.ts";
const urls = await getFilesToTransform();
console.info(`Benchmark for import ${urls.length} files.`);
async function nextResolve(url: string) {
try {
await access(fileURLToPath(url));
return { url, importAttributes: {} };
} catch {
throw Object.assign(new Error(), {
code: "ERR_MODULE_NOT_FOUND",
});
}
}
async function nextLoad(url: string) {
return {
format: "ts" as any,
source: await readFile(url.slice(8)),
};
}
const ctx = {} as any;
async function emulateImport(file: string) {
return load((await resolve(file, ctx, nextResolve)).url, ctx, nextLoad);
}
const fileSizeProfiler: Profiler = {
onStart: ctx => ctx.defineMetric({
key: "filesize",
format: "{dataSize}",
analysis: 1,
lowerIsBetter: true,
}),
async onCase(_, case_, metrics) {
const results = await case_.invoke() as any[];
metrics.filesize = results.reduce((v, c) => v + c.source.length, 0);
},
};
// Run benchmark: pnpm exec esbench --file loader.ts
export default defineSuite({
profilers: [fileSizeProfiler],
params: {
compiler: names,
},
baseline: {
type: "compiler",
value: names[0],
},
async setup(scene) {
await setCompiler(scene.params.compiler);
scene.benchAsync("load", () => {
tsconfckCache.clear();
typeCache.clear();
return Promise.all(urls.map(emulateImport));
});
},
});