performance.measure()
The measure()
method creates a named timestamp
in the browser's performance entry buffer between marks, the navigation start time, or the current time.
When measuring between two marks, there is a start mark and end mark, respectively. The named timestamp is referred to as a measure.
The measure
can be retrieved using any of the Performance
interfaces:
(getEntries()
,
getEntriesByName()
or
getEntriesByType()
).
Note: This feature is available in Web Workers
Syntax
measure(measureName)
measure(measureName, MeasureOptions)
measure(measureName, startMark)
measure(measureName, startMark, 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()
.
Arguments
measureName
-
A
DOMString
representing the name of the measure. MeasureOptions
Optional-
An object that may contain all measure options (the
startMark
andendMark
may be specified in this object or as their own arguments):detail
-
Arbitrary metadata to be included in the measure.
start
-
Timestamp
DOMHighResTimeStamp
to be used as the start time, orDOMString
to be used as start mark. If this represents the name of a start mark, then it is defined in the same way asstartMark
(in other words it must be the name of an existing mark or aPerformanceTiming
property). duration
-
Duration between the start and end mark times (
DOMHighResTimeStamp
). end
-
Timestamp (
DOMHighResTimeStamp
) to be used as the end time, orDOMString
to be used as end mark. If this represents the name of an end mark, then it is defined in the same way asendMark
(in other words it must be the name of an existing mark or aPerformanceTiming
property).
startMark
Optional-
A
DOMString
representing the name of the measure's starting mark. May also be the name of aPerformanceTiming
property. Specifying a name that does not represent an existingPerformanceMark
orPerformanceTiming
raises aSyntaxError
DOMException
. endMark
Optional-
A
DOMString
representing the name of the measure's ending mark. This may also be the name of aPerformanceTiming
property. Specifying a name that does not represent an existingPerformanceMark
orPerformanceTiming
raises aSyntaxError
DOMException
.
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
DOMException
-
This can be thrown in any case where the start, end or duration might be ambiguous:
- Both
endMark
andMeasureOptions
are specified. MeasureOptions
is specified without eitherstart
andend
members.MeasureOptions
is specified with all ofstart
,end
, andduration
members (which might then be inconsistent).
- 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.
Example
The following example shows how measure()
is used to create a new measure performance entry
in the browser's performance entry buffer.
const markerNameA = "example-marker-a"
const markerNameB = "example-marker-b"
// Run some nested timeouts, and create a PerformanceMark for each.
performance.mark(markerNameA);
setTimeout(function() {
performance.mark(markerNameB);
setTimeout(function() {
// Create a variety of measurements.
performance.measure("measure a to b", markerNameA, markerNameB);
performance.measure("measure a to now", markerNameA);
performance.measure("measure from navigation start to b", undefined, markerNameB);
performance.measure("measure from navigation start to now");
// Pull out all of the measurements.
console.log(performance.getEntriesByType("measure"));
// Finally, clean up the entries.
performance.clearMarks();
performance.clearMeasures();
}, 1000);
}, 1000);
Specifications
Specification |
---|
User Timing # dom-performance-measure |
Browser compatibility
BCD tables only load in the browser