Skip to the code

Rate a metric

Write rate(name, value). name is one of the five metrics the web-vitals library rates: "LCP", "INP", "CLS", "FCP", or "TTFB". value is what a page measured: milliseconds for the four timings, and a score for CLS. Return the rating web-vitals gives that value, as the strings it uses: "good", "needs-improvement", or "poor".

Each metric has two thresholds. A value up to the first is good, a value up to the second needs improvement, and a value above the second is poor, so a value right on a threshold gets the better rating. Use the thresholds web-vitals uses; the first hint has them if you don't know them by heart.

Throw an Error whose message is Unknown metric "<name>" for any other name. That includes "FID": web-vitals no longer measures it, since INP replaced it as the Core Web Vital for responsiveness. Throw a RangeError when value isn't a finite number of 0 or more.

rate("LCP", 1800); // "good": the tea shop's home page
rate("INP", 350); // "needs-improvement"
rate("CLS", 0.31); // "poor"
rate("LCP", 2500); // "good": right on the threshold
rate("FID", 40); // throws Error: Unknown metric "FID"

What the tests check

  1. rates a value inside each band
    expect(rate("LCP", 1200)).toBe("good");
    expect(rate("LCP", 3100)).toBe("needs-improvement");
    expect(rate("LCP", 5200)).toBe("poor");
    
  2. a value right on a threshold gets the better rating
    expect(rate("LCP", 2500)).toBe("good");
    expect(rate("LCP", 4000)).toBe("needs-improvement");
    expect(rate("INP", 200)).toBe("good");
    expect(rate("INP", 500)).toBe("needs-improvement");
    expect(rate("TTFB", 1800)).toBe("needs-improvement");
    
  3. INP, FCP, and TTFB each have thresholds of their own
    expect(rate("INP", 201)).toBe("needs-improvement");
    expect(rate("INP", 501)).toBe("poor");
    expect(rate("FCP", 1800)).toBe("good");
    expect(rate("FCP", 1801)).toBe("needs-improvement");
    expect(rate("FCP", 3001)).toBe("poor");
    expect(rate("TTFB", 800)).toBe("good");
    expect(rate("TTFB", 1200)).toBe("needs-improvement");
    expect(rate("TTFB", 1801)).toBe("poor");
    
  4. CLS is a score, not milliseconds
    expect(rate("CLS", 0)).toBe("good");
    expect(rate("CLS", 0.1)).toBe("good");
    expect(rate("CLS", 0.11)).toBe("needs-improvement");
    expect(rate("CLS", 0.25)).toBe("needs-improvement");
    expect(rate("CLS", 0.26)).toBe("poor");
    expect(rate("CLS", 3)).toBe("poor");
    
  5. a name web-vitals doesn't rate is an error, FID and lowercase names too
    expect(() => rate("FID", 40)).toThrow('Unknown metric "FID"');
    expect(() => rate("lcp", 1200)).toThrow('Unknown metric "lcp"');
    expect(() => rate("toString", 1)).toThrow('Unknown metric "toString"');
    
  6. a value that isn't a finite number of 0 or more is a RangeError
    expect(() => rate("LCP", -1)).toThrow(RangeError);
    expect(() => rate("LCP", NaN)).toThrow(RangeError);
    expect(() => rate("LCP", "2500")).toThrow(RangeError);
    expect(() => rate("CLS", Infinity)).toThrow(RangeError);
    expect(rate("TTFB", 0)).toBe("good");
    

Learn more

solution.jsSaved in this browser

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).