Skip to main content
InterviewHubPRO
Section Overview
Q7

Question 7 of 50

Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

14%
Web Vitals & Performance (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

How does the browser measure Web Vitals programmatically (`PerformanceObserver`)?

junior LevelCode ExampleFormsPerformance

In Simple Words: The modern browser exposes the PerformanceObserver API:

javascript
// Observing Largest Contentful Paint (LCP)
const observer = new PerformanceObserver((entryList) => {
  const entries = entryList.getEntries();
  const lastEntry = entries[entries.length - 1];
  console.log('LCP Candidate:', lastEntry.renderTime || lastEntry.loadTime);
});
observer.observe({ type: 'largest-contentful-paint', buffered: true });

buffered: true retrieves performance entries that occurred before the script initialized.