Lighthouse's Performance score is a weighted average of five metric scores, each from 0 to 1. Write performanceScore(scores). scores has each metric's score by its acronym: FCP, SI (Speed Index), LCP, TBT, and CLS, and sometimes INP. A score is null when that metric's audit failed. Return the number the report's gauge shows: a whole number from 0 to 100, or null.
Lighthouse 13.6.0's default config weighs FCP 10, SI 10, LCP 25, TBT 30, and CLS 25. INP weighs 0, so it never counts, even when it's null. Multiply each score by its weight, add them up, and divide by the total weight, 100. Lighthouse rounds that to two decimals with Math.round(mean * 100) / 100, and the gauge shows Math.round(score * 100). If any metric that has a weight is null, return null.
performanceScore({ FCP: 0.92, SI: 0.85, LCP: 0.61, TBT: 0.4, CLS: 1, INP: null });
// 9.2 + 8.5 + 15.25 + 12 + 25 = 69.95, so a mean of 0.6995, a score of 0.7, and 70 on the gauge
expect(performanceScore({ FCP: 1, SI: 1, LCP: 1, TBT: 1, CLS: 1 })).toBe(100);
expect(performanceScore({ FCP: 0, SI: 0, LCP: 0, TBT: 0, CLS: 0 })).toBe(0);
const perfect = { FCP: 1, SI: 1, LCP: 1, TBT: 1, CLS: 1 };
expect(performanceScore({ ...perfect, TBT: 0 })).toBe(70);
expect(performanceScore({ ...perfect, LCP: 0 })).toBe(75);
expect(performanceScore({ ...perfect, CLS: 0 })).toBe(75);
expect(performanceScore({ ...perfect, FCP: 0 })).toBe(90);
expect(performanceScore({ ...perfect, SI: 0 })).toBe(90);
expect(performanceScore({ FCP: 0.92, SI: 0.85, LCP: 0.61, TBT: 0.4, CLS: 1 })).toBe(70);
expect(performanceScore({ FCP: 0.9, SI: 0.8, LCP: 0.6, TBT: 0.5, CLS: 1 })).toBe(72);
expect(performanceScore({ FCP: 0.81, SI: 0.77, LCP: 0.66, TBT: 0.93, CLS: 0.88 })).toBe(82);
const scores = { FCP: 0.9, SI: 0.8, LCP: 0.6, TBT: 0.5, CLS: 1 };
expect(performanceScore({ ...scores, INP: 0 })).toBe(72);
expect(performanceScore({ ...scores, INP: null })).toBe(72);
expect(performanceScore({ ...scores, INP: 1 })).toBe(72);
expect(performanceScore({ FCP: 0.9, SI: 0.8, LCP: 0.6, TBT: null, CLS: 1 })).toBe(null);
expect(performanceScore({ FCP: 1, SI: 1, LCP: null, TBT: 1, CLS: 1, INP: 1 })).toBe(null);
expect(performanceScore({ FCP: 0.98, SI: 0.97, LCP: 0.78, TBT: 0.79, CLS: 0.99 })).toBe(87);
expect(performanceScore({ FCP: 0.83, SI: 0.83, LCP: 0.9, TBT: 0.9, CLS: 0.9 })).toBe(89);
expect(performanceScore({ FCP: 0.95, SI: 1, LCP: 1, TBT: 1, CLS: 1 })).toBe(100);
expect(performanceScore({ FCP: 1, SI: 0.45, LCP: 1, TBT: 1, CLS: 1 })).toBe(95);
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 Lighthouse's Performance score. 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.