Skip to content

Commit 2f1e7d7

Browse files
committed
Format and lint
1 parent f6b50ff commit 2f1e7d7

22 files changed

+279
-209
lines changed

.eslintrc.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ module.exports = {
1313
},
1414
rules: {
1515
"@typescript-eslint/no-explicit-any": "off",
16-
"no-constant-condition": "off"
16+
"no-constant-condition": "off",
17+
"@typescript-eslint/no-empty-function": "off",
18+
"no-async-promise-executor": "off"
1719
},
1820
env: {
1921
browser: true,

@types/global.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/cli/collect.ts

Lines changed: 85 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,99 @@
11
import { BenchmarkResult, Mode, Benchmark } from "@types";
2-
import { ARRAY_ACTIVE, INDEX, COUNT, CHUNK_SIZE, COLLECT_TIMEOUT, DEVIATION_MAX, MATCH_NUMBER, NS_IN_SECOND, ARRAY } from "@constants";
2+
import {
3+
ARRAY_ACTIVE,
4+
INDEX,
5+
COUNT,
6+
CHUNK_SIZE,
7+
COLLECT_TIMEOUT,
8+
DEVIATION_MAX,
9+
MATCH_NUMBER,
10+
NS_IN_SECOND,
11+
ARRAY,
12+
} from "@constants";
313
import { thread } from "./thread.js";
414
import { getBenchmarkResult, isNumberValid, shiftArray } from "@utils";
5-
import {pub} from "ueve/async";
6-
import {$collectEnd, $collectStart, $iterationEnd, $iterationStart} from "@events";
7-
8-
export const collect = (benchmark: Benchmark, mode: Mode) => new Promise<BenchmarkResult>(async (resolve) => {
9-
const timeout = process.hrtime.bigint() + BigInt((COLLECT_TIMEOUT * NS_IN_SECOND) / 1000);
10-
const worker = await thread(benchmark, mode);
11-
12-
const start = async () => {
13-
await pub($iterationStart, { benchmark, mode });
14-
15-
worker.postMessage(null);
16-
}
17-
18-
const end = async (benchmarkResult: BenchmarkResult, isValid: boolean) => {
19-
await pub($iterationEnd, { benchmark, mode, median: benchmarkResult.median, isValid });
20-
await pub($collectEnd, { benchmark, mode, result: benchmarkResult });
21-
22-
resolve(benchmarkResult);
23-
worker.terminate();
24-
};
25-
26-
await pub($collectStart, { benchmark, mode });
27-
28-
INDEX[0] = 0;
29-
COUNT[0] = 0;
30-
31-
worker.on("message", async (v) => {
32-
const index = INDEX[0];
33-
const count = COUNT[0];
34-
const isValid = isNumberValid(v);
35-
36-
ARRAY.copyWithin(CHUNK_SIZE, 0, CHUNK_SIZE);
37-
38-
if(index && !(index % CHUNK_SIZE)) {
39-
const benchmarkResult = getBenchmarkResult();
40-
const percent = benchmarkResult.deviation.standard.percent;
41-
42-
if((mode === "ram" && benchmarkResult.median === 0) || percent <= DEVIATION_MAX) {
43-
if(count + 1 === MATCH_NUMBER) {
44-
await end(benchmarkResult, isValid);
15+
import { pub } from "ueve/async";
16+
import {
17+
$collectEnd,
18+
$collectStart,
19+
$iterationEnd,
20+
$iterationStart,
21+
} from "@events";
22+
23+
export const collect = (benchmark: Benchmark, mode: Mode) =>
24+
new Promise<BenchmarkResult>(async (resolve) => {
25+
const timeout =
26+
process.hrtime.bigint() + BigInt((COLLECT_TIMEOUT * NS_IN_SECOND) / 1000);
27+
const worker = await thread(benchmark, mode);
28+
29+
const start = async () => {
30+
await pub($iterationStart, { benchmark, mode });
31+
32+
worker.postMessage(null);
33+
};
34+
35+
const end = async (benchmarkResult: BenchmarkResult, isValid: boolean) => {
36+
await pub($iterationEnd, {
37+
benchmark,
38+
mode,
39+
median: benchmarkResult.median,
40+
isValid,
41+
});
42+
await pub($collectEnd, { benchmark, mode, result: benchmarkResult });
43+
44+
resolve(benchmarkResult);
45+
worker.terminate();
46+
};
47+
48+
await pub($collectStart, { benchmark, mode });
49+
50+
INDEX[0] = 0;
51+
COUNT[0] = 0;
52+
53+
worker.on("message", async (v) => {
54+
const index = INDEX[0];
55+
const count = COUNT[0];
56+
const isValid = isNumberValid(v);
57+
58+
ARRAY.copyWithin(CHUNK_SIZE, 0, CHUNK_SIZE);
59+
60+
if (index && !(index % CHUNK_SIZE)) {
61+
const benchmarkResult = getBenchmarkResult();
62+
const percent = benchmarkResult.deviation.standard.percent;
63+
64+
if (
65+
(mode === "ram" && benchmarkResult.median === 0) ||
66+
percent <= DEVIATION_MAX
67+
) {
68+
if (count + 1 === MATCH_NUMBER) {
69+
await end(benchmarkResult, isValid);
70+
}
71+
72+
COUNT[0] += 1;
73+
} else {
74+
COUNT[0] = 0;
4575
}
76+
}
4677

47-
COUNT[0] += 1;
48-
} else {
49-
COUNT[0] = 0;
78+
if (process.hrtime.bigint() >= timeout) {
79+
await end(getBenchmarkResult(), isValid);
5080
}
51-
}
5281

53-
if (process.hrtime.bigint() >= timeout) {
54-
await end(getBenchmarkResult(), isValid);
55-
}
82+
if (isValid) {
83+
if (index >= CHUNK_SIZE) {
84+
shiftArray(ARRAY_ACTIVE);
5685

57-
if(isValid) {
58-
if (index >= CHUNK_SIZE) {
59-
shiftArray(ARRAY_ACTIVE);
86+
ARRAY_ACTIVE[CHUNK_SIZE - 1] = v;
87+
} else {
88+
ARRAY_ACTIVE[index] = v;
89+
}
6090

61-
ARRAY_ACTIVE[CHUNK_SIZE - 1] = v;
62-
} else {
63-
ARRAY_ACTIVE[index] = v;
91+
INDEX[0] += 1;
6492
}
6593

66-
INDEX[0] += 1;
67-
}
94+
await pub($iterationEnd, { benchmark, mode, median: v, isValid });
95+
await start();
96+
});
6897

69-
await pub($iterationEnd, { benchmark, mode, median: v, isValid });
7098
await start();
7199
});
72-
73-
await start();
74-
});

src/cli/loadDirectory.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
import {$directoryClose, $directoryOpen} from "@events";
2-
import {Content, Directory} from "@types";
3-
import {isDirectory, joinPath} from "@utils";
4-
import {readdir} from "fs/promises";
5-
import {pub} from "ueve/async";
6-
import {loadFile} from "./loadFile.js";
7-
8-
export const loadDirectory = async (root: string, input: string[]): Promise<Directory> => {
9-
const name = root.split("/").at(root.at(-1) === "/" ? -2: -1) as string;
1+
import { $directoryClose, $directoryOpen } from "@events";
2+
import { Content, Directory } from "@types";
3+
import { isDirectory, joinPath } from "@utils";
4+
import { readdir } from "fs/promises";
5+
import { pub } from "ueve/async";
6+
import { loadFile } from "./loadFile.js";
7+
8+
export const loadDirectory = async (
9+
root: string,
10+
input: string[],
11+
): Promise<Directory> => {
12+
const name = root.split("/").at(root.at(-1) === "/" ? -2 : -1) as string;
1013
const promises: Promise<Content>[] = [];
1114

1215
await pub($directoryOpen, { root, input });
1316

1417
for (let i = 0; i < input.length; ++i) {
1518
const path = joinPath(root, input[i]);
1619

17-
if(await isDirectory(path)) {
18-
promises.push(new Promise<Directory>(async (resolve) => {
19-
const files = await readdir(path);
20-
const folder = await loadDirectory(path, files);
20+
if (await isDirectory(path)) {
21+
promises.push(
22+
new Promise<Directory>(async (resolve) => {
23+
const files = await readdir(path);
24+
const folder = await loadDirectory(path, files);
2125

22-
resolve(folder);
23-
}));
26+
resolve(folder);
27+
}),
28+
);
2429
} else {
2530
promises.push(loadFile(path));
2631
}
@@ -34,6 +39,6 @@ export const loadDirectory = async (root: string, input: string[]): Promise<Dire
3439
type: "directory",
3540
name,
3641
path: root,
37-
content
42+
content,
3843
};
3944
};

src/cli/loadFile.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,55 @@ import { CACHE_DIR, FILE_CACHE } from "@constants";
44
import { writeFile } from "node:fs/promises";
55
import { getType } from "@utils";
66
import { Benchmark, File } from "@types";
7-
import {pub} from "ueve/async";
8-
import {$compilationEnd, $compilationStart, $fileClose, $fileOpen} from "@events";
7+
import { pub } from "ueve/async";
8+
import {
9+
$compilationEnd,
10+
$compilationStart,
11+
$fileClose,
12+
$fileOpen,
13+
} from "@events";
914

1015
export const loadFile = async (path: string): Promise<File> => {
1116
await pub($fileOpen, { path });
1217

13-
if(!FILE_CACHE.has(path)) {
18+
if (!FILE_CACHE.has(path)) {
1419
await pub($compilationStart, { path });
1520

1621
const name = path.split("/").at(-1) as string;
1722
const outPath = `${CACHE_DIR}${randomUUID()}.mjs`;
1823
const output = await transformFile(path, {
1924
jsc: {
2025
parser: {
21-
syntax: path.endsWith(".ts") ? "typescript": "ecmascript"
26+
syntax: path.endsWith(".ts") ? "typescript" : "ecmascript",
2227
},
23-
target: "esnext"
24-
}
28+
target: "esnext",
29+
},
2530
});
2631

2732
await writeFile(outPath, output.code);
2833

2934
const module = await import(outPath);
3035
const benchmarks: Benchmark[] = [];
3136

32-
if(typeof module.default === "function") {
37+
if (typeof module.default === "function") {
3338
benchmarks.push({
3439
name,
3540
path: "default",
3641
fn: module.default,
3742
type: getType(module.default),
38-
file: outPath
43+
file: outPath,
3944
});
4045
} else {
41-
for(const name in module) {
42-
if(name[0] === "$") {
46+
for (const name in module) {
47+
if (name[0] === "$") {
4348
const fn = module[name];
4449

4550
benchmarks.push({
4651
name,
4752
path: name,
4853
fn,
4954
type: getType(fn),
50-
file: outPath
55+
file: outPath,
5156
});
5257
}
5358
}
@@ -58,7 +63,7 @@ export const loadFile = async (path: string): Promise<File> => {
5863
name,
5964
path: path,
6065
file: outPath,
61-
benchmarks
66+
benchmarks,
6267
});
6368

6469
await pub($compilationEnd, { path });

src/cli/runBenchmark.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import {BENCHMARK_RUNS} from "@constants";
2-
import {$benchmarkEnd, $benchmarkStart, $runEnd, $runStart} from "@events";
3-
import {Benchmark, BenchmarkResult, BenchmarkResults} from "@types";
4-
import {pub} from "ueve/async";
5-
import {collect} from "./collect.js";
1+
import { BENCHMARK_RUNS } from "@constants";
2+
import { $benchmarkEnd, $benchmarkStart, $runEnd, $runStart } from "@events";
3+
import { Benchmark, BenchmarkResult, BenchmarkResults } from "@types";
4+
import { pub } from "ueve/async";
5+
import { collect } from "./collect.js";
66

77
export async function runBenchmark(benchmark: Benchmark) {
88
await pub($benchmarkStart, { benchmark });
99

1010
let cpu: null | BenchmarkResult = null;
1111
let ram: null | BenchmarkResult = null;
1212

13-
for(let i = 0; i < BENCHMARK_RUNS; ++i) {
13+
for (let i = 0; i < BENCHMARK_RUNS; ++i) {
1414
await pub($runStart, { benchmark, index: i });
1515

1616
const cpuResult = await collect(benchmark, "cpu");
1717
const ramResult = await collect(benchmark, "ram");
1818

19-
if(cpu === null || cpu.median > cpuResult.median) {
19+
if (cpu === null || cpu.median > cpuResult.median) {
2020
cpu = cpuResult;
2121
}
2222

23-
if(ram === null || ram.median > ramResult.median) {
23+
if (ram === null || ram.median > ramResult.median) {
2424
ram = ramResult;
2525
}
2626

src/cli/runDirectory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Directory} from "@types";
2-
import {runFile} from "./runFile.js";
1+
import { Directory } from "@types";
2+
import { runFile } from "./runFile.js";
33

44
export async function runDirectory(directory: Directory) {
5-
for(const content of directory.content) {
6-
if(content.type === "directory") await runDirectory(content);
5+
for (const content of directory.content) {
6+
if (content.type === "directory") await runDirectory(content);
77
else await runFile(content);
88
}
99
}

src/cli/runFile.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import {ASYNC_CPU, ASYNC_RAM, SYNC_CPU, SYNC_RAM} from "@constants";
2-
import {File} from "@types";
3-
import {runBenchmark} from "./runBenchmark.js";
1+
import { ASYNC_CPU, ASYNC_RAM, SYNC_CPU, SYNC_RAM } from "@constants";
2+
import { File } from "@types";
3+
import { runBenchmark } from "./runBenchmark.js";
44

55
export async function runFile(file: File) {
6-
for(const benchmark of file.benchmarks) {
6+
for (const benchmark of file.benchmarks) {
77
const { cpu, ram } = await runBenchmark(benchmark);
88
const isAsync = benchmark.type === "async";
9-
const minCpu = isAsync ? ASYNC_CPU: SYNC_CPU;
10-
const minRam = isAsync ? ASYNC_RAM: SYNC_RAM;
9+
const minCpu = isAsync ? ASYNC_CPU : SYNC_CPU;
10+
const minRam = isAsync ? ASYNC_RAM : SYNC_RAM;
1111

12-
if(cpu.median < minCpu[0]) {
12+
if (cpu.median < minCpu[0]) {
1313
minCpu[0] = cpu.median;
1414
}
1515

16-
if(ram.median < minRam[0]) {
16+
if (ram.median < minRam[0]) {
1717
minRam[0] = ram.median;
1818
}
1919
}

0 commit comments

Comments
 (0)