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

View in English Always switch to English

Performance.measure()

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月⁩.

measure() 方法在浏览器性能记录缓存中创建了一个名为时间戳的记录来记录两个特殊标志位(通常称为开始标志和结束标志)。被命名的时间戳称为一次测量(measure)。

备注: 此特性在 Web Worker 中可用。

measure 可以被 Performance 接口 getEntries*() 中的方法检查到 (getEntries(), getEntriesByName() 或者 getEntriesByType()).

The measure's performance entry will have the following property values:

语法

js
measure(measureName)
measure(measureName, startMark)
measure(measureName, startMark, endMark)
measure(measureName, measureOptions)
measure(measureName, measureOptions, endMark)

参数

name

一个 DOMString,代表测量的名字。

startMark 可选

一个 DOMString,代表测量的开始标志名字。May also be the name of a PerformanceTiming property.

endMark 可选

一个DOMString,代表测量的结束标志名字。May also be the name of a PerformanceTiming property.

返回值

例子

以下例子展示如何在浏览器性能记录缓存中使用 measure()创建一个新的测量记录performance entry

js
// 以一个标志开始。
performance.mark("mySetTimeout-start");

// 等待一些时间。
setTimeout(function () {
  // 标志时间的结束。
  performance.mark("mySetTimeout-end");

  // 测量两个不同的标志。
  performance.measure("mySetTimeout", "mySetTimeout-start", "mySetTimeout-end");

  // 获取所有的测量输出。
  // 在这个例子中只有一个。
  var measures = performance.getEntriesByName("mySetTimeout");
  var measure = measures[0];
  console.log("setTimeout milliseconds:", measure.duration);

  // 清除存储的标志位
  performance.clearMarks();
  performance.clearMeasures();
}, 1000);

规范

Specification
User Timing
# dom-performance-measure

浏览器兼容性