Performance: mark() 方法
注意:此功能在 Web Workers 中可用。
mark() 方法会创建一个命名的 PerformanceMark 对象,该对象表示浏览器性能时间线中的一个高精度时间戳标记。
语法
js
mark(name)
mark(name, markOptions)
参数
name-
一个代表标记名称的字符串。其名称不能与已弃用的
PerformanceTiming接口的某个属性名称相同。 markOptions可选-
一个用于为标记指定时间戳和其他元数据的对象。
detail可选-
要包含在标记中的任意元数据。默认为
null。必须是 结构克隆算法 支持的。 startTime可选-
DOMHighResTimeStamp,用作标记时间。默认为performance.now()。
返回值
创建的 PerformanceMark 条目。
异常
SyntaxError:如果name是已弃用的PerformanceTiming接口的某个属性,则抛出此错误。请参阅 下面的示例。TypeError:如果startTime为负数,则抛出此错误。
示例
创建命名标记
以下示例使用 mark() 来创建命名的 PerformanceMark 条目。您可以创建多个同名标记。您还可以为它们赋值,以便拥有对已创建 PerformanceMark 对象的引用。
js
performance.mark("login-started");
performance.mark("login-started");
performance.mark("login-finished");
performance.mark("form-sent");
const videoMarker = performance.mark("video-loaded");
创建带有详细信息的标记
性能标记是可配置的,可以使用 markOptions 对象,您可以在其中将附加信息放入 detail 属性中,该属性可以是任何类型。
js
performance.mark("login-started", {
detail: "Login started using the login button in the top menu.",
});
performance.mark("login-started", {
detail: { htmlElement: myElement.id },
});
创建具有不同开始时间的标记
mark() 方法的默认时间戳是 performance.now()。您可以使用 markOptions 中的 startTime 选项将其设置为其他时间。
js
performance.mark("start-checkout", {
startTime: 20.0,
});
performance.mark("login-button-pressed", {
startTime: myEvent.timeStamp,
});
保留名称
注意,为了向后兼容,不能使用已弃用的 PerformanceTiming 接口中的名称。以下示例会抛出错误:
js
performance.mark("navigationStart");
// SyntaxError: "navigationStart" is part of
// the PerformanceTiming interface,
// and cannot be used as a mark name
规范
| 规范 |
|---|
| 用户计时 # dom-performance-mark |
浏览器兼容性
加载中…