Performance.mark()

mark() 方法在浏览器的性能缓冲区中使用给定名称添加一个timestamp(时间戳)

应用定义的时间戳可以通过 Performance 接口的一个 getEntries*() 方法 (getEntries(), getEntriesByName() 或者 getEntriesByType() (en-US)) 检索到。

标记performance entry将具有以下属性值:

如果这个方法被指定的 name 已经存在于PerformanceTiming 接口,会抛出一个SyntaxError错误。

语法

performance.mark(name);

参数

name

一个表示标记名称的DOMString

返回值

实例

下面的示例演示如何使用 mark() 来创建和检索PerformanceMark (en-US)条目。

js
// 创建一些标记。
performance.mark("squirrel");
performance.mark("squirrel");
performance.mark("monkey");
performance.mark("monkey");
performance.mark("dog");
performance.mark("dog");

// 获取所有的 PerformanceMark 条目。
const allEntries = performance.getEntriesByType("mark");
console.log(allEntries.length);
// 6

// 获取所有的 "monkey" PerformanceMark 条目。
const monkeyEntries = performance.getEntriesByName("monkey");
console.log(monkeyEntries.length);
// 2

// 删除所有标记。
performance.clearMarks();

规范

Specification
User Timing
# dom-performance-mark

浏览器兼容性

BCD tables only load in the browser