Performance.measure()

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

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

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

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

语法

performance.measure(name, startMark, 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

浏览器兼容性

BCD tables only load in the browser