JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsperformance
Next

Learn the concept

Core Web Vitals & Performance Metrics

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 VitalsJavaScript
// 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'] });

Real-World Applications

Use Cases

Optimizing an e-commerce product page

To ensure images load quickly (LCP), interactions like 'Add to Cart' are responsive (INP), and no layout shifts occur, leading to higher conversion rates and improved SEO.

Improving a news website's article pages

To provide a smooth reading experience by minimizing loading times, ensuring text is interactive without delay, and preventing sudden content jumps (CLS), which directly impacts user engagement and ad revenue.

Enhancing a web application's user dashboard

Where critical data loads fast, interactive elements respond instantly, and the layout remains stable during data updates, improving user productivity and satisfaction.

Mini Projects

Web Vitals Monitoring Dashboard

intermediate

Build a simple dashboard that monitors and visualizes Core Web Vitals metrics for a given URL using web-vitals library and a charting library.

Optimized Image Gallery

beginner

Create an image gallery and optimize it for LCP by implementing responsive images, lazy loading, and appropriate preloading strategies.

Industry Examples

Google Search

Google uses Core Web Vitals as a ranking signal, making it crucial for any website aiming for good visibility in search results. Their own properties (e.g., Google Search, YouTube) are heavily optimized for these metrics.

Amazon

Amazon prioritizes web performance to drive sales. Their product pages and checkout flows are highly optimized for fast loading (LCP), responsive interactions (INP), and visual stability (CLS) to reduce bounce rates and increase conversions.

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?
Next