Which budget lines does this build break?
Leaf and Kettle's CI checks one build of the tea page against six budget lines, each the way its tool checks it: size-limit 14.3.0 on the files' Brotli sizes, with limits written as text; webpack 5's default performance.maxAssetSize on the files it writes, which only warns; Lighthouse CI on three lab runs of LCP; and the 75th percentile of last week's phone visits. Which lines break, and what does it log?
// One build of Leaf and Kettle's tea page. "brotli" is a file compressed with Brotli, as size-limit measures it;
// "emitted" is the file as the bundler writes it to disk, minified but not compressed.
const files = {
"app.js": { brotli: 81900, emitted: 268000 },
"vendor.js": { brotli: 20500, emitted: 151000 },
"site.css": { brotli: 10240, emitted: 61000 },
};
const lcpRuns = [2380, 2710, 2650]; // three Lighthouse CI runs of this build, in ms
const fieldLcp = [1900, 2600, 2200, 2400, 3100, 2350, 2700, 2450]; // last week's phone visits, in ms
// size-limit 14.3.0 reads a limit such as "100 KB": a k, M, or G is a power of 1000, or of 1024 when an i follows it.
function sizeLimitBytes(text) {
const [, number, prefix, iec] = /^(\d+(?:\.\d+)?) *(?:([kmg])?(i)?b)?$/i.exec(text);
const power = prefix ? "kmg".indexOf(prefix.toLowerCase()) + 1 : 0;
return Math.floor(Number(number) * (iec ? 1024 : 1000) ** power);
}
// Lighthouse CI combines an assertion's runs with its aggregationMethod, "optimistic" unless it names another.
function lhciValue(runs, aggregationMethod = "optimistic") {
const sorted = [...runs].sort((a, b) => a - b);
if (aggregationMethod === "median") {
const middle = Math.floor((sorted.length - 1) / 2);
return sorted.length % 2 === 1 ? sorted[middle] : (sorted[middle] + sorted[middle + 1]) / 2;
}
// For a maximum, optimistic keeps the lowest value and pessimistic the highest.
return aggregationMethod === "optimistic" ? sorted[0] : sorted[sorted.length - 1];
}
// Field data is judged at the 75th percentile, by nearest rank.
const p75 = (samples) => [...samples].sort((a, b) => a - b)[Math.ceil(0.75 * samples.length) - 1];
// [line, measured, limit, unit, what going over does]
const budget = [
["size-limit, JavaScript at load", files["app.js"].brotli + files["vendor.js"].brotli, sizeLimitBytes("100 KB"), "bytes", "breaks"],
["size-limit, CSS at load", files["site.css"].brotli, sizeLimitBytes("10 KiB"), "bytes", "breaks"],
["webpack maxAssetSize, app.js", files["app.js"].emitted, 250000, "bytes", "warns"],
["Lighthouse CI LCP, no aggregationMethod", lhciValue(lcpRuns), 2500, "ms", "breaks"],
["Lighthouse CI LCP, median", lhciValue(lcpRuns, "median"), 2500, "ms", "breaks"],
["Field LCP on phones, p75", p75(fieldLcp), 2500, "ms", "breaks"],
];
const broken = [];
const warned = [];
budget.forEach(([line, measured, limit, unit, overDoes], index) => {
// Every one of these checks passes a value that's exactly on its limit.
const over = measured > limit;
if (over) (overDoes === "breaks" ? broken : warned).push(index + 1);
console.log(`${index + 1}. ${line}: ${measured} of ${limit} ${unit}, ${over ? overDoes : "passes"}`);
});
console.log(`Broken: ${broken.join(", ")}. Warnings: ${warned.join(", ")}.`);
Practice it in the editor, with tests
The answer
Work out every line it logs, in order. Then check.
Checking your sign-in…