Performance.measure()

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

Note: 此特性在 Web Worker 中可用。

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

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.

返回值

void
 

例子

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

// 以一个标志开始。
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);

详细说明

详细说明 状态 评论
User Timing Level 2
measure()
Working Draft Clarifies measure() processing model.
User Timing
measure()
Recommendation Basic definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
measureChrome Full support 28
Full support 28
No support 25 — 28
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support 12Firefox Full support 41IE Full support 10Opera Full support 33Safari Full support 11WebView Android Full support 46Chrome Android Full support 28
Full support 28
No support 25 — 28
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android Full support 42Opera Android Full support 33Safari iOS Full support 11Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.