Lab Data vs. Field RUM
Synthetic lab tools (Lighthouse, WebPageTest) run in controlled environments and are indispensable for debugging regressions before deployment. However, only Real User Monitoring (RUM) captures actual user devices, cellular latency, and browser cache hit rates.
Capturing Core Web Vitals with PerformanceObserver
Browsers expose standard Performance Timeline entries directly to JavaScript without requiring heavy monitoring SDKs:
// Observe Largest Contentful Paint
new PerformanceObserver((entryList) => {
const entries = entryList.getEntries();
const lastEntry = entries[entries.length - 1];
if (lastEntry) {
console.log('LCP candidate (ms):', Math.round(lastEntry.startTime), lastEntry.element);
}
}).observe({ type: 'largest-contentful-paint', buffered: true });
// Observe Layout Shifts
let clsScore = 0;
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
if (!entry.hadRecentInput) {
clsScore += entry.value;
}
}
}).observe({ type: 'layout-shift', buffered: true });
Send RUM beacons using navigator.sendBeacon() inside a visibilitychange listener (document.visibilityState === 'hidden') so telemetry never competes with page loading or blocks navigation.
Setting Enforceable Performance Budgets
Define clear quantitative ceilings in CI so gradual asset creep is caught automatically:
- Initial HTML (compressed):
≤ 25 KB - Critical CSS (compressed):
≤ 20 KB - Total Initial JavaScript (compressed):
≤ 60 KB - Hero LCP Image:
≤ 120 KB - Field p75 Targets:
LCP ≤ 2.5s,INP ≤ 200ms,CLS ≤ 0.1