What does this Lighthouse score calculation print?
The code scores one mobile Lighthouse run of the tea page the way Lighthouse 13.6.0 does: each metric on its log-normal curve, with the bands, the boost above 0.9, and rounding down, then the weighted Performance score. It also scores the same LCP on the desktop curve. What does it print?
// Lighthouse 13.6.0's control points for a mobile run: the value at p10 scores 0.9, the median 0.5.
const mobile = {
FCP: { p10: 1800, median: 3000 },
SI: { p10: 3387, median: 5800 },
LCP: { p10: 2500, median: 4000 },
TBT: { p10: 200, median: 600 },
CLS: { p10: 0.1, median: 0.25 },
};
const desktopLCP = { p10: 1200, median: 2400 };
const weights = { FCP: 10, SI: 10, LCP: 25, TBT: 30, CLS: 25 };
// The error function, as Lighthouse approximates it (Abramowitz and Stegun, formula 7.1.26).
function erf(x) {
const sign = Math.sign(x);
const ax = Math.abs(x);
const t = 1 / (1 + 0.3275911 * ax);
const y = t * (0.254829592 + t * (-0.284496736 + t * (1.421413741 + t * (-1.453152027 + t * 1.061405429))));
return sign * (1 - y * Math.exp(-ax * ax));
}
function metricScore({ p10, median }, value) {
if (value <= 0) return 1;
// The share of the log-normal curve that's slower than this value.
const z = (Math.log(value / median) * 0.9061938024368232) / -Math.log(p10 / median);
const raw = (1 - erf(z)) / 2;
// Keep the score in the band its value falls in.
let score;
if (value <= p10) score = Math.max(0.9, Math.min(1, raw));
else if (value <= median) score = Math.max(0.5, Math.min(0.8999999999999999, raw));
else score = Math.max(0, Math.min(0.49999999999999994, raw));
// A small boost above 0.9, then two decimals, rounded down.
if (score > 0.9) score += 0.05 * (score - 0.9);
return Math.floor(score * 100) / 100;
}
// One mobile Lighthouse run of Leaf and Kettle's tea page: milliseconds, and a score for CLS.
const run = { FCP: 1800, SI: 5800, LCP: 3000, TBT: 0, CLS: 0.05 };
let points = 0;
for (const [name, value] of Object.entries(run)) {
const score = metricScore(mobile[name], value);
points += score * weights[name];
console.log(`${name} ${value}: ${score}`);
}
console.log(`The same LCP on desktop: ${metricScore(desktopLCP, run.LCP)}`);
const categoryScore = Math.round((points / 100) * 100) / 100;
console.log(`Points: ${points} of 100`);
console.log(`Performance score: ${Math.round(categoryScore * 100)}`);
Practice it in the editor, with tests
The answer
Work out every line it logs, in order. Then check.
Checking your sign-in…