Skip to content

Commit

Permalink
chore: switch to tsx cli from esbuild-register and to mitata from ben…
Browse files Browse the repository at this point in the history
…chmark.js (#107)
  • Loading branch information
DylanPiercey authored Jun 1, 2022
1 parent 2b6bcbd commit 5906367
Show file tree
Hide file tree
Showing 7 changed files with 1,747 additions and 1,100 deletions.
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"extension": ["js", "ts"],
"enable-source-maps": true,
"watchFiles": ["src/**/*.ts", "src/**/*.marko"],
"require": ["esbuild-register", "mocha-snap"]
"require": ["tsx", "mocha-snap"]
}
50 changes: 50 additions & 0 deletions bench.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import "./build.mts";

import os from "os";
import path from "path";
import fs from "fs/promises";
import cp from "child_process";
import { promisify } from "util";

import degit from "degit";
import { group, bench, run } from "mitata";

const exec = promisify(cp.exec);
const api = (await import("./dist/index.mjs")) as unknown as typeof import("./src");
const FIXTURES = path.resolve("src/__tests__/fixtures");
const GREP = new RegExp(process.env.GREP || ".", "g");
const COMPARE = process.env.COMPARE;
let compareAPI: undefined | typeof api;

if (COMPARE) {
const cwd = path.join(os.tmpdir(), `htmljs-bench-${COMPARE}`);
const exists = await fs.mkdir(cwd).catch(() => true);
if (!exists) {
await degit(`marko-js/htmljs-parser#${COMPARE}`).clone(cwd);
await exec("npm ci && npm run --if-present build", { cwd });
}

const pkg = JSON.parse(
await fs.readFile(path.join(cwd, "package.json"), "utf-8")
);
compareAPI = await import(
path.join(cwd, pkg.exports?.["."].import ?? pkg.main)
);
}

for (const entry of await fs.readdir(FIXTURES)) {
if (!GREP.test(entry)) continue;
const filename = path.join(FIXTURES, entry, "input.marko");
const src = await fs.readFile(filename, "utf-8");
const check = (mod: typeof api) => () => mod.createParser({}).parse(src);
if (compareAPI) {
group(() => {
bench(entry, check(api));
bench(`${entry}#${COMPARE}`, check(compareAPI));
});
} else {
bench(entry, check(api));
}
}

await run();
21 changes: 21 additions & 0 deletions build.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { build, BuildOptions } from "esbuild";

const opts: BuildOptions = {
bundle: true,
outdir: "dist",
platform: "node",
target: ["node14"],
entryPoints: ["src/index.ts"],
};

await Promise.all([
build({
...opts,
format: "cjs",
}),
build({
...opts,
format: "esm",
outExtension: { ".js": ".mjs" },
}),
]);
23 changes: 0 additions & 23 deletions build.ts

This file was deleted.

Loading

0 comments on commit 5906367

Please sign in to comment.