JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsperformance
Next
performance
junior
metrics

What are Core Web Vitals and why do they matter?

web-vitals
lcp
cls
inp
metrics
seo
Quick Answer

Core Web Vitals are Google's metrics for user experience: LCP (loading), INP (interactivity), and CLS (visual stability). They matter because they affect search rankings and directly correlate with user satisfaction and conversion rates.

Detailed Explanation

The Three Core Web Vitals:

  1. LCP (Largest Contentful Paint)

    • Measures loading performance
    • Time until largest content element visible
    • Target: < 2.5 seconds
  2. INP (Interaction to Next Paint)

    • Measures interactivity
    • Replaced FID in 2024
    • Time from interaction to visual feedback
    • Target: < 200ms
  3. CLS (Cumulative Layout Shift)

    • Measures visual stability
    • How much content shifts unexpectedly
    • Target: < 0.1

Why They Matter:

  • Google ranking factor
  • User experience correlation
  • Conversion rate impact
  • Bounce rate reduction

Code Examples

Measuring Web Vitals
// Using web-vitals library
import { onLCP, onINP, onCLS } from 'web-vitals';

function sendToAnalytics({ name, value, id }) {
  // Send to your analytics service
  console.log({ name, value, id });
  
  // Example: Send to Google Analytics
  gtag('event', name, {
    event_category: 'Web Vitals',
    event_label: id,
    value: Math.round(name === 'CLS' ? value * 1000 : value),
    non_interaction: true,
  });
}

// Measure each vital
onLCP(sendToAnalytics);
onINP(sendToAnalytics);
onCLS(sendToAnalytics);

// Using Performance Observer API directly
const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    console.log(entry.name, entry.startTime);
  }
});

observer.observe({ entryTypes: ['largest-contentful-paint'] });

Resources

Web Vitals

article

web-vitals library

docs

Related Questions

How do you optimize images for web performance?

junior
images

What is lazy loading and how do you implement it?

junior
loading
Next
How do you optimize images for web performance?