How are these five numbers rated?
One visit to the tea shop reported five metrics. The code rates each one with the thresholds web-vitals 6.2.3 uses, making the same comparisons the library makes, and logs the rating. What does it log?
// The thresholds web-vitals 6.2.3 rates each metric with: [the top of good, the top of needs improvement].
const thresholds = {
LCP: [2500, 4000],
INP: [200, 500],
CLS: [0.1, 0.25],
FCP: [1800, 3000],
TTFB: [800, 1800],
};
// The two comparisons web-vitals makes when it sets a metric's rating.
function rate(name, value) {
const [good, poor] = thresholds[name];
if (value > poor) return "poor";
if (value > good) return "needs-improvement";
return "good";
}
// One visit to Leaf and Kettle's tea page: milliseconds, and a score for CLS.
const visit = { LCP: 2500, INP: 500, CLS: 0.1, FCP: 3100, TTFB: 900 };
for (const [name, value] of Object.entries(visit)) {
console.log(`${name} ${value}: ${rate(name, value)}`);
}
Practice it in the editor, with tests
The answer
Work out every line it logs, in order. Then check.
Checking your sign-in…