大网数据广告活动
Web 标准与前端开发

Performance.measure()

· 52CE 我爱测

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

User Timing API

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

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

The measure's PerformanceEntry will have the following property values:

  • PerformanceEntry.entryType - set to "measure".
  • PerformanceEntry.name - set to the "name" given when the measure was created.
  • PerformanceEntry.startTime - set to the start mark DOMHighResTimeStamp.
  • PerformanceEntry.duration - set to a DOMHighResTimeStamp that is the duration of the measure (typically, the end mark timestamp minus the start mark timestamp).

语法

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

参数

  • name
    • : 一个 String,代表测量的名字。
  • startMark
    • : 一个 String,代表测量的开始标志名字。May also be the name of a PerformanceTiming property.
  • endMark
    • : 一个String,代表测量的结束标志名字。May also be the name of a PerformanceTiming property.

返回值

无

例子

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

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

规范

浏览器兼容性

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。