The Codex / JavaScript / Performance API

Performance API

Measuring real-world performance — performance.now(), marks, measures, PerformanceObserver, Core Web Vitals, and the Navigation Timing API.

What it is, in plain English: The Performance API provides high-resolution timing and performance measurement tools. performance.now() gives sub-millisecond timestamps. performance.mark() and performance.measure() create named timing points visible in DevTools. The PerformanceObserver watches for performance entries in real time. Core Web Vitals (LCP, FID, CLS) are the key user experience metrics measured with the Performance API.

Measuring how fast your code runs

performance.now() — precise timing

// Measures real elapsed time, sub-millisecond precision:
const start = performance.now();

doSomethingExpensive();

const elapsed = performance.now() - start;
console.log(`Took ${elapsed.toFixed(3)}ms`);

// Compare two approaches:
const t1 = performance.now();
approach1(data);
const t2 = performance.now();
approach2(data);
const t3 = performance.now();

console.log(`Approach 1: ${(t2-t1).toFixed(2)}ms`);
console.log(`Approach 2: ${(t3-t2).toFixed(2)}ms`);

Named marks and measures

// Create named timing points visible in DevTools Performance tab:
performance.mark("data:fetch:start");
const data = await fetchData();
performance.mark("data:fetch:end");

performance.mark("data:render:start");
renderData(data);
performance.mark("data:render:end");

// Create measures between marks:
performance.measure("fetch time",  "data:fetch:start",  "data:fetch:end");
performance.measure("render time", "data:render:start", "data:render:end");

// Read the measurements:
performance.getEntriesByType("measure").forEach(m => {
  console.log(`${m.name}: ${m.duration.toFixed(2)}ms`);
});
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.