此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Performance.mark()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2017年9月⁩.

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

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

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

  • entryType - 设置为 "mark".
  • name - 设置为 mark 被创建时给出的 "name"。
  • startTime - 设置为 mark() 方法被调用时的 timestamp
  • duration - 设置为 "0" (标记没有持续时间).

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

语法

js
mark(name)
mark(name, markOptions)

参数

name

一个表示标记名称的DOMString

返回值

实例

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

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

浏览器兼容性