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
DOMStringrepresenting the name of the measure. MeasureOptionsOptional-
An object that may contain all measure options (the
startMarkandendMarkmay be specified in this object or as their own arguments):detail-
Arbitrary metadata to be included in the measure.
start-
Timestamp
DOMHighResTimeStampto be used as the start time, orDOMStringto 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 aPerformanceTimingproperty). duration-
Duration between the start and end mark times (
DOMHighResTimeStamp). end-
Timestamp (
DOMHighResTimeStamp) to be used as the end time, orDOMStringto 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 aPerformanceTimingproperty).
startMarkOptional-
A
DOMStringrepresenting the name of the measure's starting mark. May also be the name of aPerformanceTimingproperty. Specifying a name that does not represent an existingPerformanceMarkorPerformanceTimingraises aSyntaxErrorDOMException. endMarkOptional-
A
DOMStringrepresenting the name of the measure's ending mark. This may also be the name of aPerformanceTimingproperty. Specifying a name that does not represent an existingPerformanceMarkorPerformanceTimingraises aSyntaxErrorDOMException.
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
timestampof a start mark, if specified inMeasureOptions.startorstartMark - a timestamp calculated from the
MeasureOptions.endandMeasureOptions.duration(ifMeasureOptions.startwas not specified) - 0, if it isn't specified and can't be determined from other values.
- a
-
duration- set to aDOMHighResTimeStampthat is the duration of the measure calculated by subtracting thestartTimefrom the end timestamp. The end timestamp is one of:- a
timestamp, if specified inMeasureOptions.end. - the
timestampof an end mark, if one is specified inMeasureOptions.endorendMark - a timestamp calculated from the
MeasureOptions.startandMeasureOptions.duration(ifMeasureOptions.endwas 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
TypeErrorDOMException-
This can be thrown in any case where the start, end or duration might be ambiguous:
- Both
endMarkandMeasureOptionsare specified. MeasureOptionsis specified without eitherstartandendmembers.MeasureOptionsis specified with all ofstart,end, anddurationmembers (which might then be inconsistent).
- Both
SyntaxErrorDOMException-
The named mark does not exist.
- An end mark is specified using either
endMarkorMeasureOptions.end, but there is noPerformanceMarkin the performance buffer with the matching name. - An end mark is specified using either
endMarkorMeasureOptions.end, but it cannot be converted to match that of a read only attribute in thePerformanceTiminginterface. - A start mark is specified using either
startMarkorMeasureOptions.start, but there is noPerformanceMarkin the performance buffer with the matching name. - A start mark is specified using either
startMarkorMeasureOptions.start, but it cannot be converted to match that of a read only attribute in thePerformanceTiminginterface.
- An end mark is specified using either
DataCloneErrorDOMException-
The
MeasureOptions.detailvalue is non-nulland cannot be serialized using the HTML "StructuredSerialize" algorithm. RangeError-
The
MeasureOptions.detailvalue is non-nulland 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