Marks & Measures
Marks & Measures
Section titled “Marks & Measures”Introduction
Section titled “Introduction”The User Timing API lets you mark specific points in time with performance.mark() and measure the duration between marks with performance.measure().
Marking Points
Section titled “Marking Points”// Mark the start of an operationperformance.mark('fetch-start');
await fetch('/api/data');
// Mark the endperformance.mark('fetch-end');
// Measure the durationperformance.measure('data-fetch', 'fetch-start', 'fetch-end');
// Get the measurementconst measures = performance.getEntriesByName('data-fetch');console.log(`Fetch took ${measures[0].duration}ms`);Multiple Measurements
Section titled “Multiple Measurements”async function loadApp() { performance.mark('app-start');
performance.mark('data-start'); const data = await fetchData(); performance.mark('data-end'); performance.measure('data-load', 'data-start', 'data-end');
performance.mark('render-start'); renderData(data); performance.mark('render-end'); performance.measure('render-time', 'render-start', 'render-end');
performance.mark('app-end'); performance.measure('total-load', 'app-start', 'app-end');
// Log all measurements performance.getEntriesByType('measure').forEach(m => { console.log(`${m.name}: ${m.duration.toFixed(2)}ms`); });}Named Marks
Section titled “Named Marks”performance.mark('step-1');// ... do work ...performance.mark('step-2');// ... do more work ...performance.mark('step-3');
performance.measure('phase-1', 'step-1', 'step-2');performance.measure('phase-2', 'step-2', 'step-3');Cleaning Up
Section titled “Cleaning Up”// Clear specific marksperformance.clearMarks('fetch-start');
// Clear all marksperformance.clearMarks();
// Clear specific measuresperformance.clearMeasures('data-fetch');
// Clear all measuresperformance.clearMeasures();Getting All Entries
Section titled “Getting All Entries”// All marksconst marks = performance.getEntriesByType('mark');
// All measuresconst measures = performance.getEntriesByType('measure');
// Named entriesconst specific = performance.getEntriesByName('data-fetch');Summary
Section titled “Summary”performance.mark(name)— timestamp a pointperformance.measure(name, startMark, endMark)— measure durationgetEntriesByType('measure')— retrieve measurementsclearMarks()/clearMeasures()— clean up