Course contentsShow
← JavaScript
Lesson 1921 of 2,72340. PerformanceFree lesson

Performance.mark and Performance.measure

Creating named markers and measuring intervals between them for structured performance profiling.

Performance.mark and Performance.measure

You'll learn how to create named markers in your code and measure the time between them for structured, reusable performance profiling.

What Are Marks and Measures?

While Performance.now() gives you precise timestamps, the User Timing API (performance.mark() and performance.measure()) lets you label specific points in your code and calculate intervals between them. Think of marks as placing sticky notes at different checkpoints in your code, then using measures to calculate how long it took to get from one sticky note to another.

This approach is cleaner than manually subtracting timestamps and makes your performance data visible in browser DevTools timelines.

Creating Marks

Use performance.mark() to place a named marker:

performance.mark('fetchStart');
// ... do some work ...
performance.mark('fetchEnd');

Measuring Between Marks

Use performance.measure() to calculate the duration between two marks:

performance.mark('processStart');
processData();
performance.mark('processEnd');

performance.measure('dataProcessing', 'processStart', 'processEnd');

The first argument is the name of your measurement, followed by the start and end mark names.

Retrieving Your Measurements

Access all marks and measures using performance.getEntriesByType():

const marks = performance.getEntriesByType('mark');
const measures = performance.getEntriesByType('measure');

measures.forEach(m => {
  console.log(`${m.name}: ${m.duration.toFixed(2)}ms`);
});

You can also retrieve a specific entry by name: performance.getEntriesByName('dataProcessing').

Why Use This Over Performance.now()?

Marks and measures integrate with browser DevTools, appear in performance timelines, and eliminate the need for manual timestamp subtraction. They're perfect for tracking named operations that happen repeatedly or across different code paths.

Key Takeaway: performance.mark() creates named checkpoints in your code, and performance.measure() calculates intervals between them, giving you structured, DevTools-friendly performance profiling.