Skip to content
jshacks.dev
Performance

Mark and measure with user timing API

Name the start and end of a task with performance.mark and performance.measure, then read the duration back or see it in the DevTools Performance panel.

1 min read
Summarize in AI

performance.now() subtraction works, but the numbers have no names. The User Timing API lets you mark points and measure between them, so the result is labelled and shows up in DevTools.

timing.jsJavaScript
performance.mark('parse-start');
JSON.parse('{"items":[1,2,3]}');
performance.mark('parse-end');

const entry = performance.measure('parse', 'parse-start', 'parse-end');
console.log(entry.name, entry.entryType, entry.duration > 0);

console.log(performance.getEntriesByName('parse').length);
Text
parse measure true
1

performance.measure returns the new PerformanceMeasure entry, and getEntriesByName finds it again later. In Chrome the marks and measures appear in the Timings track of the Performance panel, which lines your names up with the flame chart.

The entries pile up in a buffer, so call performance.clearMarks() and performance.clearMeasures() in long-running code. User Timing is in Node.js and every modern browser; see the W3C User Timing spec.

Also inAPIs
Taggednode 24
Share

More snippets

APIs

Send JSON with fetch POST

Send a JSON body with fetch POST by stringifying the object and setting the Content-Type header, since fetch does not do either of those for you.

1 min read
Performance

Time code with performance.now

Time a block of code with performance.now, which returns a monotonic high-resolution timestamp in milliseconds, and avoid the clock jumps Date.now can have.

1 min read
APIs

Fetch JSON and handle HTTP errors

Fetch JSON in the browser or Node.js 24 and throw on 404 or 500 yourself, because fetch only rejects on network failure and does not check the status.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.