mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Answer stability round 1: 0 🔴, 4 🟡 (0 misread); kill the minify and boundary mutants, derive the comparison's esbuild pin, measure the companion sizes
This commit is contained in:
@@ -3,18 +3,16 @@ import { execFileSync } from "node:child_process";
|
||||
import { gzipSync } from "node:zlib";
|
||||
|
||||
const bytesPerKilobyte = 1000;
|
||||
// A published figure stays true while it is within this share of the measure.
|
||||
const tolerance = 0.1;
|
||||
|
||||
// What a consumer's bundler ships for the package: its code and its dependencies, minified, gzipped.
|
||||
export const minifiedGzippedBytes = async (resolveDir, specifier) => {
|
||||
// What a consumer's bundler ships for the whole of the packages: their code and dependencies, minified, gzipped.
|
||||
export const minifiedGzippedBytes = async (resolveDir, specifiers) => {
|
||||
const { outputFiles } = await build({
|
||||
bundle: true,
|
||||
format: "esm",
|
||||
logLevel: "silent",
|
||||
minify: true,
|
||||
platform: "node",
|
||||
stdin: { contents: `export * from ${JSON.stringify(specifier)};`, resolveDir },
|
||||
stdin: { contents: specifiers.map((specifier) => `export * from ${JSON.stringify(specifier)};`).join("\n"), resolveDir },
|
||||
write: false,
|
||||
});
|
||||
return gzipSync(outputFiles[0].contents, { level: 9 }).length;
|
||||
@@ -33,10 +31,11 @@ const published = [
|
||||
];
|
||||
const tableCell = new RegExp(`^\\| \`@larvit/adf-codec\` \\|.*\\| ${number} kB \\|$`, "m");
|
||||
|
||||
const offBy = (claimed, bytes) => Math.abs(Number(claimed) - bytes / bytesPerKilobyte) > tolerance * bytes / bytesPerKilobyte;
|
||||
// Whole bytes keep the bound exact: 0.1 has no binary spelling.
|
||||
const offBy = (claimed, bytes) => Math.abs(Math.round(Number(claimed) * bytesPerKilobyte) - bytes) * 10 > bytes;
|
||||
const kilobytes = (bytes) => (bytes / bytesPerKilobyte).toFixed(1);
|
||||
|
||||
// The README's size claims that no longer match the measure, each as a sentence.
|
||||
// The README's size claims that are missing or more than a tenth off the measure, each as a sentence.
|
||||
export const drift = (readme, measured) => [
|
||||
...published.flatMap(([key, label, pattern]) => {
|
||||
const found = pattern.exec(readme);
|
||||
|
||||
@@ -15,15 +15,23 @@ const fixture = (files) => {
|
||||
};
|
||||
|
||||
const readmeSaying = (minified, tarball, unpacked, tableCell) =>
|
||||
`| Size | ${minified} kB minified and gzipped; ${tarball} kB tarball, ${unpacked} kB unpacked |\n` +
|
||||
`| Aspect | Contract |\n| Size | ${minified} kB minified and gzipped; ${tarball} kB tarball, ${unpacked} kB unpacked |\n` +
|
||||
`| \`@larvit/adf-codec\` | both ways | a | b | c | 0 | ${tableCell} kB |\n`;
|
||||
|
||||
const measured = { minified: 13000, tarball: 60000, unpacked: 220000 };
|
||||
|
||||
test("minified size ignores comments and whitespace", async () => {
|
||||
const bare = fixture({ "entry.js": "export const answer=40+2;" });
|
||||
const padded = fixture({ "entry.js": "// a comment\nexport const answer = 40 + 2;\n\n\n" });
|
||||
assert.equal(await minifiedGzippedBytes(padded, "./entry.js"), await minifiedGzippedBytes(bare, "./entry.js"));
|
||||
test("minified size ignores comments, whitespace and the length of local names", async () => {
|
||||
const bare = fixture({ "entry.js": "export const r=((a)=>a+1)(1);" });
|
||||
const padded = fixture({ "entry.js": "// a comment\nexport const r = ((someVeryLongParameterName) => someVeryLongParameterName + 1)(1);\n\n\n" });
|
||||
assert.equal(await minifiedGzippedBytes(padded, ["./entry.js"]), await minifiedGzippedBytes(bare, ["./entry.js"]));
|
||||
});
|
||||
|
||||
test("minified size counts every package it is given", async () => {
|
||||
const dir = fixture({
|
||||
"one.js": "export const one = 1;",
|
||||
"two.js": `export const two = ${JSON.stringify(Array.from({ length: 200 }, (_, i) => (i * 2654435761 % 4294967296).toString(36)))};`,
|
||||
});
|
||||
assert.ok(await minifiedGzippedBytes(dir, ["./one.js", "./two.js"]) > await minifiedGzippedBytes(dir, ["./one.js"]) + 500);
|
||||
});
|
||||
|
||||
test("minified size counts the dependencies a bundler pulls in", async () => {
|
||||
@@ -33,7 +41,7 @@ test("minified size counts the dependencies a bundler pulls in", async () => {
|
||||
"node_modules/dep/index.js": `export const words = ${JSON.stringify(Array.from({ length: 200 }, (_, i) => (i * 2654435761 % 4294967296).toString(36)))};`,
|
||||
"node_modules/dep/package.json": '{"name":"dep","type":"module","main":"index.js"}',
|
||||
});
|
||||
assert.ok(await minifiedGzippedBytes(withDependency, "./entry.js") > await minifiedGzippedBytes(alone, "./entry.js") + 500);
|
||||
assert.ok(await minifiedGzippedBytes(withDependency, ["./entry.js"]) > await minifiedGzippedBytes(alone, ["./entry.js"]) + 500);
|
||||
});
|
||||
|
||||
test("a README within a tenth of every measure has no drift", () => {
|
||||
@@ -41,6 +49,15 @@ test("a README within a tenth of every measure has no drift", () => {
|
||||
assert.deepEqual(drift(readmeSaying(14, 65, 240, 14), measured), []);
|
||||
});
|
||||
|
||||
test("a figure exactly a tenth off holds and a little more does not", () => {
|
||||
assert.deepEqual(drift(readmeSaying(13, 54, 198, 13), measured), []);
|
||||
assert.deepEqual(drift(readmeSaying(13, 66, 242, 13), measured), []);
|
||||
assert.deepEqual(drift(readmeSaying(13, 66.1, 242.1, 13), measured), [
|
||||
"README says 66.1 kB tarball; measured 60.0 kB",
|
||||
"README says 242.1 kB unpacked; measured 220.0 kB",
|
||||
]);
|
||||
});
|
||||
|
||||
test("a figure more than a tenth off is named with its measure", () => {
|
||||
assert.deepEqual(drift(readmeSaying(13, 60, 220, 11), measured), [
|
||||
"README's comparison table says 11 kB for @larvit/adf-codec; measured 13.0 kB",
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ import { resolve } from "node:path";
|
||||
import { drift, minifiedGzippedBytes, packedBytes } from "./figures.mjs";
|
||||
|
||||
const root = resolve(import.meta.dirname, "..");
|
||||
const measured = { minified: await minifiedGzippedBytes(root, "./dist/index.js"), ...packedBytes(root) };
|
||||
const measured = { minified: await minifiedGzippedBytes(root, ["./dist/index.js"]), ...packedBytes(root) };
|
||||
console.log(`${(measured.minified / 1000).toFixed(1)} kB minified and gzipped; ${(measured.tarball / 1000).toFixed(1)} kB tarball, ${(measured.unpacked / 1000).toFixed(1)} kB unpacked`);
|
||||
if (process.argv.includes("--check")) {
|
||||
const problems = drift(readFileSync(resolve(root, "README.md"), "utf8"), measured);
|
||||
|
||||
@@ -11,6 +11,7 @@ import * as extendedParser from "extended-markdown-adf-parser";
|
||||
import * as faulander from "@faulander/md2adf";
|
||||
import { fromADF } from "mdast-util-from-adf";
|
||||
import * as githubMarkdownAdf from "github-markdown-adf";
|
||||
import { gzipSync } from "node:zlib";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { loadAdf4j } from "@nthings.dev/adf4j-wasm";
|
||||
import * as markdownToAdf from "markdown-to-adf";
|
||||
@@ -104,6 +105,19 @@ const converters = {
|
||||
"mdast-util-from-adf": { toMarkdown: (doc) => mdastToMarkdown(fromADF(doc)) },
|
||||
};
|
||||
|
||||
// The sizes a row's figure leaves out, which the README states beside it.
|
||||
const companions = {
|
||||
"@atlaskit/editor-markdown-transformer": {
|
||||
"minified and gzipped bytes with @atlaskit/editor-json-transformer": () => minifiedGzippedBytes(process.cwd(), ["@atlaskit/editor-markdown-transformer", "@atlaskit/editor-json-transformer"]),
|
||||
},
|
||||
"@nthings.dev/adf4j-wasm": {
|
||||
"WebAssembly module gzipped bytes": () => gzipSync(readFileSync("node_modules/@nthings.dev/adf4j-wasm/adf4j-wasm.js.wasm"), { level: 9 }).length,
|
||||
},
|
||||
"mdast-util-from-adf": {
|
||||
"minified and gzipped bytes with mdast-util-to-markdown": () => minifiedGzippedBytes(process.cwd(), ["mdast-util-from-adf", "mdast-util-to-markdown"]),
|
||||
},
|
||||
};
|
||||
|
||||
const show = (label, call) => {
|
||||
try {
|
||||
const value = call();
|
||||
@@ -118,10 +132,11 @@ for (const [name, { toAdf, toMarkdown }] of Object.entries(converters)) {
|
||||
console.log(`\n## ${name} ${manifest.version}`);
|
||||
console.log("direct runtime dependencies:", Object.keys(manifest.dependencies ?? {}).length);
|
||||
try {
|
||||
console.log("minified and gzipped bytes:", await minifiedGzippedBytes(process.cwd(), name));
|
||||
console.log("minified and gzipped bytes:", await minifiedGzippedBytes(process.cwd(), [name]));
|
||||
} catch (error) {
|
||||
console.log("minified and gzipped bytes failed:", error.message.split("\n")[0]);
|
||||
console.log("bundling failed:", error.message.split("\n")[0]);
|
||||
}
|
||||
for (const [label, measure] of Object.entries(companions[name] ?? {})) console.log(`${label}:`, await measure());
|
||||
const written = toMarkdown && show("ADF to markdown", () => toMarkdown(roundTripDoc));
|
||||
if (written !== undefined && toAdf) {
|
||||
const readBack = show("read back", () => toAdf(written));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/sh
|
||||
# Keep these versions and --before in step with the README's comparison table, and esbuild's with ../bundle-size/package.json.
|
||||
# Keep these versions and --before in step with the README's comparison table.
|
||||
# --before reads a bare date as midnight UTC, so the day after the run date includes that day's publishes.
|
||||
set -eu
|
||||
here=$(cd "$(dirname "$0")" && pwd)
|
||||
@@ -18,7 +18,7 @@ docker run --rm --memory 2g --memory-swap 2g --ulimit core=0 -u "$(id -u):$(id -
|
||||
adf-to-markdown@1.0.1 \
|
||||
adf-to-md@1.2.1 \
|
||||
adf2markdown@1.2.0 \
|
||||
esbuild@0.28.2 \
|
||||
esbuild@$(node -p "require(\"/bundle-size/package.json\").devDependencies.esbuild") \
|
||||
extended-markdown-adf-parser@2.4.0 \
|
||||
github-markdown-adf@1.3.0 \
|
||||
markdown-to-adf@0.2.20 \
|
||||
|
||||
Reference in New Issue
Block a user