Skip to content

Measuring Metrics

Measuring Core Web Vitals

Core Web Vitals are critical metrics that define user experience on the web. To optimize performance, developers must measure these metrics using tools like Lighthouse, Chrome DevTools, and the Web Vitals library. Each tool provides unique insights into loading performance, interactivity, and visual stability.


## Lighthouse: Comprehensive Audit Tool

Lighthouse is a Chrome DevTools-based auditing tool that evaluates Core Web Vitals (LCP, FID, CLS) and provides actionable recommendations.

๐Ÿ” Running Lighthouse

Use the CLI or DevTools to audit a site:

npx lighthouse https://example.com --output=json --output-path=report.json

Example Output:

{
  "audits": {
    "first-contentful-paint": {
      "score": 0.95,
      "details": { "metric": "LCP", "value": 2300 }
    }
  }
}

๐Ÿ“Š Lighthouse Metrics

  • LCP (Loading Performance): Measures time to render the first contentful paint.
  • FID (Interactivity): Tracks time until first input is processed.
  • CLS (Visual Stability): Quantifies layout shifts.

Diagram: Lighthouse Audit Interface (https://developers.google.com/web/tools/lighthouse)


## Chrome DevTools: Real-Time Performance Analysis

Chrome DevTools provides granular insights into Core Web Vitals during runtime.

๐Ÿ“ˆ Performance Tab Workflow

  1. Open DevTools > Performance tab.
  2. Click Record and load the page.
  3. Analyze the Metrics section for LCP, FID, and CLS.

Example:
- LCP: Look for the first contentful paint in the Waterfall timeline.
- FID: Check the User Timing section for input delays.
- CLS: Use the Layout Shifts panel to identify unstable elements.

๐Ÿงช Using the Performance API

Programmatically access metrics with the Performance API:

performance.getEntriesByType('layout-shift').forEach(entry => {
  console.log(`CLS: ${entry.metrics.layoutShift.value.toFixed(2)}px`);
});

Diagram: Chrome DevTools Performance Tab (https://developer.chrome.com/docs/devtools/performance/)


## Web Vitals Library: Client-Side Tracking

The Web Vitals library allows developers to track Core Web Vitals directly in the browser.

๐Ÿ“ฆ Installation and Setup

npm install web-vitals
import { onLCP, onFID, onCLS } from 'web-vitals';

onLCP((metric) => {
  console.log(`LCP: ${metric.value.toFixed(2)}ms`);
});

onFID((metric) => {
  console.log(`FID: ${metric.value.toFixed(2)}ms`);
});

onCLS((metric) => {
  console,log(`CLS: ${metric.value.toFixed(2)}px`);
});

๐Ÿ“Š Metrics Details

  • LCP: Measures time to first contentful paint.
  • FID: Tracks time until first input is processed.
  • CLS: Quantifies layout shifts.

Example: Logging FID for a React app:

onFID((metric) => {
  if (metric.value > 100) {
    console.warn('FID exceeded 100ms!');
  }
});


Key takeaways

  • Use Lighthouse for automated audits and benchmarking.
  • Leverage Chrome DevTools for real-time, granular analysis.
  • Integrate the Web Vitals library for client-side tracking and custom alerts.
  • Combine all tools to identify bottlenecks and validate optimizations.
  • Prioritize LCP, FID, and CLS to ensure a seamless user experience.