Performance: measure() method
The measure()
method creates a named PerformanceMeasure
object representing a time measurement between two marks in the browser's performance timeline.
When measuring between two marks, there is a start mark and end mark, respectively. The named timestamp is referred to as a measure.
Syntax
js
measure(measureName)
measure(measureName, startMark)
measure(measureName, startMark, endMark)
measure(measureName, measureOptions)
measure(measureName, measureOptions, endMark)
If only measureName
is specified, the start timestamp is set to zero, and the end timestamp (which is used to calculate the duration) is the value that would be returned by Performance.now()
.
You can use strings to identify PerformanceMark
objects as start and end marks.
To only provide an endMark
, you need to provide an empty measureOptions
object:
performance.measure("myMeasure", {}, "myEndMarker")
.
Parameters
measureName
-
A string representing the name of the measure.
measureOptions
Optional-
An object that may contain measure options.
detail
Optional-
Arbitrary metadata to be included in the measure. Defaults to
null
. Must be structured-cloneable. start
Optional-
Timestamp (
DOMHighResTimeStamp
) to be used as the start time, or string that names aPerformanceMark
to use for the start time.If this is a string naming a
PerformanceMark
, then it is defined in the same way asstartMark
. duration
Optional-
Duration between the start and end mark times. If omitted, this defaults to
performance.now()
; the time that has elapsed since the context was created. If provided, you must also specify eitherstart
orend
but not both. end
Optional-
Timestamp (
DOMHighResTimeStamp
) to be used as the end time, or string that names aPerformanceMark
to use for the end time.If this is a string naming a
PerformanceMark
, then it is defined in the same way asendMark
.
startMark
Optional-
A string naming a
PerformanceMark
in the performance timeline. ThePerformanceEntry.startTime
property of this mark will be used for calculating the measure. endMark
Optional-
A string naming a
PerformanceMark
in the performance timeline. ThePerformanceEntry.startTime
property of this mark will be used for calculating the measure. If you want to pass this argument, you must also pass eitherstartMark
or an emptymeasureOptions
object.
Return value
The PerformanceMeasure
entry that was created.
The returned measure will have the following property values:
entryType
- set to "measure
".name
- set to the "name
" argument.startTime
- set to:- a
timestamp
, if specified inmeasureOptions.start
. - the
timestamp
of a start mark, if specified inmeasureOptions.start
orstartMark
- a timestamp calculated from the
measureOptions.end
andmeasureOptions.duration
(ifmeasureOptions.start
was not specified) - 0, if it isn't specified and can't be determined from other values.
- a
-
duration
- set to aDOMHighResTimeStamp
that is the duration of the measure calculated by subtracting thestartTime
from the end timestamp. The end timestamp is one of:- a
timestamp
, if specified inmeasureOptions.end
. - the
timestamp
of an end mark, if one is specified inmeasureOptions.end
orendMark
- a timestamp calculated from the
measureOptions.start
andmeasureOptions.duration
(ifmeasureOptions.end
was not specified) - the value returned by
Performance.now()
, if no end mark is specified or can be determined from other values.
- a
detail
- set to the value passed inmeasureOptions
.
Exceptions
TypeError
-
This can be thrown in any case where the start, end or duration might be ambiguous:
- Both
endMark
andmeasureOptions
are specified. measureOptions
is specified withduration
but without specifying eitherstart
orend
.measureOptions
is specified with all ofstart
,end
, andduration
.
- Both
SyntaxError
DOMException
-
The named mark does not exist.
- An end mark is specified using either
endMark
ormeasureOptions.end
, but there is noPerformanceMark
in the performance buffer with the matching name. - An end mark is specified using either
endMark
ormeasureOptions.end
, but it cannot be converted to match that of a read only attribute in thePerformanceTiming
interface. - A start mark is specified using either
startMark
ormeasureOptions.start
, but there is noPerformanceMark
in the performance buffer with the matching name. - A start mark is specified using either
startMark
ormeasureOptions.start
, but it cannot be converted to match that of a read only attribute in thePerformanceTiming
interface.
- An end mark is specified using either
DataCloneError
DOMException
-
The
measureOptions.detail
value is non-null
and cannot be serialized using the HTML "StructuredSerialize" algorithm. RangeError
-
The
measureOptions.detail
value is non-null
and memory cannot be allocated during serialization using the HTML "StructuredSerialize" algorithm.
Examples
Measuring duration between named markers
Given two of your own markers "login-started"
and "login-finished"
, you can create a measurement called "login-duration"
as shown in the following example. The returned PerformanceMeasure
object will then provide a duration
property to tell you the elapsed time between the two markers.
js
const loginMeasure = performance.measure(
"login-duration",
"login-started",
"login-finished"
);
console.log(loginMeasure.duration);
Measuring duration with custom start and end times
To do more advanced measurements, you can pass a measureOptions
parameter. For example, you can use the event.timeStamp
property from a click
event as the start time.
js
performance.measure("login-click", {
start: myClickEvent.timeStamp,
end: myMarker.startTime,
});
Providing additional measurement details
You can use the details
property to provide additional information of any type. Maybe you want to record which HTML element was clicked, for example.
js
performance.measure("login-click", {
detail: { htmlElement: myElement.id },
start: myClickEvent.timeStamp,
end: myMarker.startTime,
});
Specifications
Specification |
---|
User Timing # dom-performance-measure |
Browser compatibility
BCD tables only load in the browser