One Lighthouse run is noisy, so performance work runs it several times and keeps one run to report. Not the run with the median score: Lighthouse 13.6.0's computeMedianRun (core/lib/median-run.js, which Lighthouse's docs point Node scripts to) keeps the run closest to the medians of two metrics. Write medianRun(runs). Each run is a Lighthouse result, where audits["first-contentful-paint"].numericValue is its First Contentful Paint and audits.interactive.numericValue its Time to Interactive, both in ms. Return the run itself, not a copy, and leave the array in its order.
Take the median of the runs' FCPs and the median of their TTIs: sort the values, then take the middle one, or the mean of the two in the middle when there's an even number. A run's distance is (medianFcp - fcp) ** 2 + (medianTti - tti) ** 2, the square of its straight-line distance from the two medians. Return the run with the smallest distance. When two tie, return the one with the lower TTI, and when that ties too, the one that comes first.
Throw an Error with the message No runs provided for an empty array. Lighthouse reads each value as numericValue || NaN, so a missing audit, a missing numericValue, and a value of 0 all count as missing. Throw Some runs were missing an FCP value if any run's FCP is missing, and otherwise Some runs were missing a TTI value if any run's TTI is.
const run = (fcp, tti, score) => ({
categories: { performance: { score } },
audits: { "first-contentful-paint": { numericValue: fcp }, interactive: { numericValue: tti } },
});
const runs = [run(1200, 3900, 0.84), run(1500, 5200, 0.7), run(1100, 3600, 0.88), run(1300, 4100, 0.66), run(2400, 7800, 0.52)];
medianRun(runs);
// runs[3]: the medians are an FCP of 1300 and a TTI of 4100, so it's at distance 0.
// (The median score, 0.7, is runs[1]'s.)
const run = (fcp, tti, score = 0.8) => ({ categories: { performance: { score } }, audits: { "first-contentful-paint": { numericValue: fcp }, interactive: { numericValue: tti } } });
const runs = [run(1200, 3900, 0.84), run(1500, 5200, 0.7), run(1100, 3600, 0.88), run(1300, 4100, 0.66), run(2400, 7800, 0.52)];
expect(medianRun(runs)).toBe(runs[3]);
const only = run(1800, 5000);
expect(medianRun([only])).toBe(only);
const run = (fcp, tti, score = 0.8) => ({ categories: { performance: { score } }, audits: { "first-contentful-paint": { numericValue: fcp }, interactive: { numericValue: tti } } });
// The medians are 1300 and 4300. runs[0] is 500 ms off in TTI alone; runs[1] is 300 ms off in each.
const runs = [run(1300, 4800, 0.8), run(1600, 4600, 0.7), run(2000, 4300, 0.75), run(1000, 3000, 0.9), run(1100, 3500, 0.85)];
expect(medianRun(runs)).toBe(runs[1]);
const run = (fcp, tti, score = 0.8) => ({ categories: { performance: { score } }, audits: { "first-contentful-paint": { numericValue: fcp }, interactive: { numericValue: tti } } });
// The medians are 1400 and 4200, which no run has.
const runs = [run(1200, 3400, 0.9), run(1600, 5000, 0.6), run(1100, 4600, 0.75), run(1800, 3800, 0.8)];
expect(medianRun(runs)).toBe(runs[2]);
const run = (fcp, tti, score = 0.8) => ({ categories: { performance: { score } }, audits: { "first-contentful-paint": { numericValue: fcp }, interactive: { numericValue: tti } } });
const pair = [run(1000, 5000, 0.9), run(1400, 4600, 0.7)];
expect(medianRun(pair)).toBe(pair[1]);
const twins = [run(1200, 4000), run(1200, 4000)];
expect(medianRun(twins)).toBe(twins[0]);
const run = (fcp, tti, score = 0.8) => ({ categories: { performance: { score } }, audits: { "first-contentful-paint": { numericValue: fcp }, interactive: { numericValue: tti } } });
const runs = [run(2400, 7800), run(1100, 3600), run(1300, 4100)];
expect(medianRun(runs)).toBe(runs[2]);
expect(runs.map((item) => item.audits.interactive.numericValue)).toEqual([7800, 3600, 4100]);
expect(() => medianRun([])).toThrow("No runs provided");
const run = (fcp, tti, score = 0.8) => ({ categories: { performance: { score } }, audits: { "first-contentful-paint": { numericValue: fcp }, interactive: { numericValue: tti } } });
const good = run(1200, 4000);
expect(() => medianRun([good, { audits: { interactive: { numericValue: 4000 } } }])).toThrow("Some runs were missing an FCP value");
expect(() => medianRun([good, { audits: { "first-contentful-paint": {}, interactive: { numericValue: 4000 } } }])).toThrow("Some runs were missing an FCP value");
expect(() => medianRun([good, run(0, 4000)])).toThrow("Some runs were missing an FCP value");
expect(() => medianRun([good, { audits: { "first-contentful-paint": { numericValue: 1200 } } }])).toThrow("Some runs were missing a TTI value");
expect(() => medianRun([run(1200, 0), { audits: {} }])).toThrow("Some runs were missing an FCP value");
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Pick the median Lighthouse run. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.