User Timing API
The User Timing API allows you measure the performance of applications using high-precision timestamps that are part of the browser's performance timeline. There are two types of timing performance entries:
PerformanceMark
entries are marks that you can name and add at any location in an application.PerformanceMeasure
entries are time measurements between two marks.
This document provides an overview how to work with the mark and measure performance entry types. For more details and example code, see Using the User Timing API.
Performance marks
Interface: PerformanceMark
The PerformanceMark
interface has the following read-only properties (directly or inherited from PerformanceEntry
):
Creating performance marks
performance.mark()
adds a performance mark to the browser's performance timeline.PerformanceMark()
constructs aPerformanceMark
object that isn't added to the browser's performance timeline.
Retrieving performance marks
performance.getEntriesByType("mark")
gets all marks.performance.getEntriesByName("myMarker", "mark")
gets all marks with the name "myMarker".performance.getEntries()
gets all entries (filter as needed).
Removing performance marks
performance.clearMarks("myMarker")
removes the performance marker with the name "myMarker".performance.clearMarks()
removes all performance markers.
Performance measures
Interface: PerformanceMeasure
The PerformanceMeasure
interface has the following read-only properties (directly or inherited from PerformanceEntry
):
detail
provides additional information about the measure.duration
is aDOMHighResTimeStamp
that is the duration of the measure (typically, the end mark timestamp minus the start mark timestamp).entryType
is always"measure"
.name
is the name given when the measure was created.startTime
is thetimestamp
whenmeasure()
was called.
Creating performance measures
performance.measure(measureName, startMarkName, endMarkName)
creates a measure wheremeasureName
is the measure's name andstartMarkName
andendMarkName
are the start and end names, respectively, of the marks the measure will be placed between (in the performance timeline).
Retrieving performance measures
performance.getEntriesByType("measure")
gets all measures.performance.getEntriesByName("myMeasure", "measure")
gets all measures with the name "myMeasure".performance.getEntries()
gets all entries (filter as needed).
Removing performance measures
performance.clearMeasures("myMeasure")
removes the performance measure with the name "myMeasure".performance.clearMeasures()
removes all performance measures.
Examples
Measuring login duration
Typically, you identify the most critical paths of your application and measure how long it takes from start to finish. For example, you can measure how long it takes to login:
// Place at a location in the code that starts login
performance.mark("login-started");
// Place at a location in the code that finishes login
performance.mark("login-finished");
// Measure login duration
const loginMeasure = performance.measure(
"login-duration",
"login-started",
"login-finished"
);
// Send to analytics endpoint
// or log to the console
console.log(loginMeasure.duration);