web api instance method
Performance: measure() method
Available in workers
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
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 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.
-
measureOptionsOptional- : An object that may contain measure options.
-
detailOptional- : Arbitrary metadata to be included in the measure. Defaults to
null. Must be structured-cloneable.devtools- : Some browsers have use a structured
devtoolsobject within thedetailobject as part of an Extensibility API that surfaces these in custom tracks in performance traces. See the Chrome’s Extensibility API documentation for more information.dataTypeExperimental- : String with a value of
track-entry(for defining a new track) ormarker(for defining an entry in a track).
- : String with a value of
colorOptional Experimental- : Defaults to
"primary". Must be one of"primary","primary-light","primary-dark","secondary","secondary-light","secondary-dark","tertiary","tertiary-light","tertiary-dark","error".
- : Defaults to
trackOptional Experimental- : String of the name of the custom track (required for
track-entry)
- : String of the name of the custom track (required for
trackGroupOptional Experimental- : String of the name of the grouping withing a custom track (required for
track-entry)
- : String of the name of the grouping withing a custom track (required for
propertiesOptional Experimental- : Array of key-value pairs. Values can be any JSON-compatible type.
tooltipTextOptional Experimental- : Short description for tooltip.
- : Some browsers have use a structured
- : Arbitrary metadata to be included in the measure. Defaults to
-
startOptional-
: Timestamp (
DOMHighResTimeStamp) to be used as the start time, or string that names aPerformanceMarkto use for the start time.If this is a string naming a
PerformanceMark, then it is defined in the same way asstartMark.
-
-
durationOptional- : Duration (in milliseconds) between the start and end mark times. If omitted, this defaults to
now(); the time that has elapsed since the context was created. If provided, you must also specify eitherstartorendbut not both.
- : Duration (in milliseconds) between the start and end mark times. If omitted, this defaults to
-
endOptional-
: Timestamp (
DOMHighResTimeStamp) to be used as the end time, or string that names aPerformanceMarkto use for the end time.If this is a string naming a
PerformanceMark, then it is defined in the same way asendMark.
-
-
- : An object that may contain measure options.
-
startMarkOptional- : A string naming a
PerformanceMarkin the performance timeline. ThestartTimeproperty of this mark will be used for calculating the measure.
- : A string naming a
-
endMarkOptional- : A string naming a
PerformanceMarkin the performance timeline. ThestartTimeproperty of this mark will be used for calculating the measure. If you want to pass this argument, you must also pass eitherstartMarkor an emptymeasureOptionsobject.
- : A string naming a
Return value
The PerformanceMeasure entry that was created.
The returned measure will have the following property values:
-
entryType- set to"measure". -
name- set to thenameargument. -
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
now(), if no end mark is specified or can be determined from other values.
- a
-
detail- set to the value passed inmeasureOptions.
Exceptions
-
- : This can be thrown in any case where the start, end or duration might be ambiguous:
- Both
endMarkandmeasureOptionsare specified. measureOptionsis specified withdurationbut without specifying eitherstartorend.measureOptionsis specified with all ofstart,end, andduration.
- Both
- : This can be thrown in any case where the start, end or duration might be ambiguous:
-
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
- : The named mark does not exist.
-
DataCloneErrorDOMException- : The
measureOptions.detailvalue is non-nulland cannot be serialized using the HTML “StructuredSerialize” algorithm.
- : The
-
- : The
measureOptions.detailvalue is non-nulland memory cannot be allocated during serialization using the HTML “StructuredSerialize” algorithm.
- : The
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.
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.
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.
performance.measure("login-click", {
detail: { htmlElement: myElement.id },
start: myClickEvent.timeStamp,
end: myMarker.startTime,
});
DevTools Extensibility API
For browsers that support the Extensibility API you can use the detail parameter to provide more details in a devtools object that will be used to display this in performance profiles:
const imageProcessingTimeStart = performance.now();
// ... later in your code
performance.measure("Image Processing Complete", {
start: imageProcessingTimeStart,
end: performance.now(),
detail: {
// This data appears in the "Summary"
extraInfo: {
imageId: "xyz-123",
source: "cache",
checkUrl: "https://example.com/check/xyz-123",
},
// The devtools object controls the track visualization
devtools: {
dataType: "track-entry",
track: "Image Processing Tasks",
trackGroup: "My Tracks",
color: "tertiary-dark",
properties: [
["Filter Type", "Gaussian Blur"],
// Values can be objects, arrays, or other types
["Resize Dimensions", { w: 500, h: 300 }],
// String values that are URLs get linkified
["Image URL", "https://example.com/img.png"],
],
tooltipText: "Image processed successfully",
},
},
});