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.
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.
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);parse measure true
1performance.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.