JS Guide
HomeQuestionsSearchResources
Search

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsperformance
Prev
performance
senior
monitoring

How do you set up performance monitoring for production applications?

monitoring
rum
analytics
alerting
web-vitals
Quick Answer

Use Real User Monitoring (RUM) to collect Web Vitals from actual users. Tools like Google Analytics, Datadog, or custom solutions track metrics over time. Set up alerting for regressions, segment data by device/geography, and correlate with business metrics.

Detailed Explanation

Monitoring Approaches:

  1. Synthetic Monitoring:

    • Lighthouse CI
    • WebPageTest
    • Scheduled tests
  2. Real User Monitoring (RUM):

    • Actual user data
    • Device/network variety
    • Geographic distribution

Key Metrics:

  • Core Web Vitals (LCP, INP, CLS)
  • Time to First Byte (TTFB)
  • First Contentful Paint (FCP)
  • Custom business metrics

Tools:

  • Google Analytics 4
  • Datadog RUM
  • New Relic
  • Sentry Performance
  • Custom with web-vitals

Code Examples

Custom RUM with web-vitals
import { onLCP, onINP, onCLS, onFCP, onTTFB } from 'web-vitals';

// Send metrics to analytics
function sendToAnalytics(metric) {
  const body = JSON.stringify({
    name: metric.name,
    value: metric.value,
    rating: metric.rating, // 'good', 'needs-improvement', 'poor'
    delta: metric.delta,
    id: metric.id,
    navigationType: metric.navigationType,
    // Additional context
    url: window.location.href,
    userAgent: navigator.userAgent,
    connection: navigator.connection?.effectiveType,
    deviceMemory: navigator.deviceMemory,
    timestamp: Date.now(),
  });

  // Use sendBeacon for reliability
  if (navigator.sendBeacon) {
    navigator.sendBeacon('/api/analytics', body);
  } else {
    fetch('/api/analytics', {
      method: 'POST',
      body,
      keepalive: true,
    });
  }
}

// Collect all Core Web Vitals
onLCP(sendToAnalytics);
onINP(sendToAnalytics);
onCLS(sendToAnalytics);
onFCP(sendToAnalytics);
onTTFB(sendToAnalytics);

// Custom performance marks
performance.mark('app-interactive');
performance.measure('time-to-interactive', 'navigationStart', 'app-interactive');

const tti = performance.getEntriesByName('time-to-interactive')[0];
sendToAnalytics({ name: 'TTI', value: tti.duration });

Resources

Web.dev - Measure performance

article

Performance Budget Calculator

docs

Related Questions

What are Core Web Vitals and why do they matter?

junior
metrics
Previous
What is the Critical Rendering Path and how do you optimize it?