性能时间线

W3C 候选推荐草案

关于本文档的更多详情
本版本:
https://www.w3.org/TR/2025/CRD-performance-timeline-20250521/
最新发布版本:
https://www.w3.org/TR/performance-timeline/
最新编辑草案:
https://w3c.github.io/performance-timeline/
历史记录:
https://www.w3.org/standards/history/performance-timeline
提交历史
测试套件:
https://github.com/web-platform-tests/wpt/tree/master/performance-timeline
实现报告:
https://wpt.fyi/results/performance-timeline
编辑:
Nicolás Peña Moreno (Google)
前编辑:
Ilya Grigorik (Google)
(微软公司) (至2014年11月)
Zhiheng Wang (Google) (至2013年7月)
反馈:
GitHub w3c/performance-timeline (拉取请求, 新建问题, 开放问题)

摘要

本规范扩展了高精度时间规范 [HR-TIME-3],通过提供方法来存储和检索高精度性能指标数据。

本文档状态

本节描述了本文档在发布时的状态。当前 W3C 发布文档及本技术报告的最新修订可在 W3C 标准与草案索引 页面 https://www.w3.org/TR/ 查阅。

本《性能时间线》规范取代了第一版 [PERFORMANCE-TIMELINE],并包括:

本文档由 网页性能工作组推荐标准流程 发布为候选推荐草案。

作为候选推荐发布,并不代表 W3C 及其成员的认可。候选推荐草案整合了工作组打算在后续候选推荐快照中包含的先前候选推荐版本的更改。

本文档为草稿,有可能随时更新、替换或废弃。仅作为正在进行中的工作引用合适,不应作为正式引用。

本文档由遵循 W3C 专利政策的工作组制作。 W3C 维护着 公开的专利披露列表, 涉及该工作组的成果;该页面还包含了披露专利的说明。任何个人如确实知晓某专利且认为其包含 必要权利要求 ,须根据 《W3C专利政策》第6节 披露相关信息。

本文档受 2023年11月3日 W3C流程文件 管辖。

1. 简介

本节为非规范性内容。

准确衡量 Web 应用的性能特性是提升 Web 应用速度的重要因素。本规范定义了必要的 性能时间线 原语,使 Web 开发者能够访问、监控和获取 Web 应用全生命周期中的各种性能指标。

[NAVIGATION-TIMING-2]、[RESOURCE-TIMING-2] 和 [USER-TIMING-2] 是分别定义与文档导航、页面资源和开发者脚本相关的计时信息的规范实例。这些及其他性能接口共同定义了描述 Web 应用 性能时间线 的性能指标。例如,下面的脚本展示了开发者如何访问 性能时间线,以获取与文档导航、页面资源和开发者脚本相关的性能指标:

<!doctype html>
<html>
<head></head>
<body onload="init()">
  <img id="image0" src="https://www.w3.org/Icons/w3c_main.png" />
  <script>
    function init() {
      // 见 [[USER-TIMING-2]]
      performance.mark("startWork");
      doWork(); // 一些开发者代码
      performance.mark("endWork");
      measurePerf();
    }
    function measurePerf() {
      performance
        .getEntries()
        .map(entry => JSON.stringify(entry, null, 2))
        .forEach(json => console.log(json));
    }
  </script>
  </body>
</html>

另外,开发者可以通过 性能时间线,通过 PerformanceObserver 接口,接收新产生的性能指标以及(可选)已缓冲的指定类型性能指标的通知。

PerformanceObserver 接口被添加,旨在解决第一个示例中基于缓冲区方式的局限。通过使用 PerformanceObserver 接口,应用可以:

鼓励开发者在可能的情况下使用 PerformanceObserver。此外,新的性能 API 和指标可能只通过 PerformanceObserver 接口提供。该观察者通过在构造函数中指定回调,并通过 observe() 方法指定所关注的性能条目类型来工作。 用户代理决定何时执行回调,并将已经排队的性能条目传递给回调。

使用 PerformanceObserver 接口时,初始页面加载有特殊考虑:必须有一个活动的注册才能接收事件,但注册脚本可能不可用或不希望处于关键路径。为此,用户代理会在页面构建期间缓冲一定数量的事件,这些缓冲的事件可通过注册观察者时设置 buffered 标志来访问。 设置此标志时,用户代理会检索并分派所缓冲的、指定条目类型的事件,并在 observe() 调用后的第一次回调中传递它们。

注意

缓冲事件的数量由定义该指标的规范决定,缓冲旨在用于前 N 个事件;缓冲不是无限或持续的。

<!doctype html>
<html>
<head></head>
<body>
<img id="image0" src="https://www.w3.org/Icons/w3c_main.png" />
<script>
// 知道我们希望使用的条目类型不被支持时。
function detectSupport(entryTypes) {
  for (const entryType of entryTypes) {
    if (!PerformanceObserver.supportedEntryTypes.includes(entryType)) {
      // 向客户端分析指示 |entryType| 不被支持。
    }
  }
}
detectSupport(["resource", "mark", "measure"]);
const userTimingObserver = new PerformanceObserver(list => {
  list
    .getEntries()
    // 获取我们关注的值
    .map(({ name, entryType, startTime, duration }) => {
      const obj = {
        "Duration": duration,
        "Entry Type": entryType,
        "Name": name,
        "Start Time": startTime,
      };
      return JSON.stringify(obj, null, 2);
    })
    // 输出到控制台。
    .forEach(console.log);
  // 处理完事件后断开连接。
  userTimingObserver.disconnect();
});
// 订阅 User-Timing 的新事件。
userTimingObserver.observe({entryTypes: ["mark", "measure"]});
const resourceObserver = new PerformanceObserver(list => {
  list
    .getEntries()
    // 获取我们关注的值
    .map(({ name, startTime, fetchStart, responseStart, responseEnd }) => {
      const obj = {
        "Name": name,
        "Start Time": startTime,
        "Fetch Start": fetchStart,
        "Response Start": responseStart,
        "Response End": responseEnd,
      };
      return JSON.stringify(obj, null, 2);
    })
    // 输出到控制台。
    .forEach(console.log);
  // 处理完事件后断开连接。
  resourceObserver.disconnect();
});
// 获取缓冲事件并订阅 Resource Timing 的新事件。
resourceObserver.observe({type: "resource", buffered: true});
</script>
</body>
</html>

2. 一致性

除非特别标注为非规范性,所有编写指南、图示、示例和注释均为非规范性内容。本规范中的其他内容均为规范性内容。

本文档中的关键字 MUSTMUST NOTSHOULDBCP 14 [RFC2119] [RFC8174] 的规定进行解释,仅当这些词以大写形式出现时,才按此解释。

以算法或特定步骤表述的一致性要求可用任何方式实现,只要最终结果等效即可。(特别地,本规范定义的算法旨在易于理解,并非强调性能。)

3. 性能时间线

每个 全局对象 包含:

每个 Document 包含:

要获取 相关性能条目元组,输入 entryTypeglobalObject,执行以下步骤:

  1. map 为与 globalObject 关联的 性能条目缓冲区映射
  2. 返回 获取条目值的结果,输入为 mapentryType 作为

3.1 扩展 Performance 接口

这是对来自 [HR-TIME-3] 的 Performance 接口的扩展,包含用于从 性能时间线 获取性能指标数据的相关属性和方法。

WebIDLpartial interface Performance {
  PerformanceEntryList getEntries ();
  PerformanceEntryList getEntriesByType (DOMString type);
  PerformanceEntryList getEntriesByName (DOMString name, optional DOMString type);
};
typedef sequence<PerformanceEntry> PerformanceEntryList;

PerformanceEntryList 表示 PerformanceEntry 的一组序列,提供开发者在 JavaScript 数组上的所有便捷方法。

3.1.1 getEntries() 方法

返回一个 PerformanceEntryList 对象,由 按名称和类型过滤缓冲区映射 算法返回,其中 nametype 均为 null

3.1.2 getEntriesByType() 方法

返回一个 PerformanceEntryList 对象,由 按名称和类型过滤缓冲区映射算法返回,其中 name 设为 nulltype 设为方法输入的 type 参数。

3.1.3 getEntriesByName() 方法

返回一个 PerformanceEntryList 对象,由 按名称和类型过滤缓冲区映射算法返回,其中 name 设为方法输入的 name 参数,type 若省略可选参数 entryType 则设为 null,否则设为方法输入的 type 参数。

4. PerformanceEntry 接口

PerformanceEntry 接口承载各种性能指标的数据。

WebIDL[Exposed=(Window,Worker)]
interface PerformanceEntry {
  readonly    attribute unsigned long long  id;
  readonly    attribute DOMString           name;
  readonly    attribute DOMString           entryType;
  readonly    attribute DOMHighResTimeStamp startTime;
  readonly    attribute DOMHighResTimeStamp duration;
  readonly    attribute unsigned long long  navigationId;
  [Default] object toJSON();
};
name
此属性必须返回其初始化值。它表示该 PerformanceEntry 对象的标识符。该标识符不要求唯一。
entryType
此属性必须返回其初始化值。
注意

所有 entryType 值都在相关注册表中定义。 例如:"mark""measure" [USER-TIMING-2],"navigation" [NAVIGATION-TIMING-2], 以及 "resource" [RESOURCE-TIMING-2]。

startTime
此属性必须返回其初始化值。它表示该性能指标首次记录的时间值。
duration
duration 属性的 getter 步骤为:当 this结束时间 为 0 时返回 0;否则返回 this结束时间 - thisstartTime
navigationId
此属性 MUST 返回其初始化值。

调用 toJSON 时,执行 [WebIDL] 的 默认 toJSON 步骤

PerformanceEntry 有一个 DOMHighResTimeStamp 结束时间, 初始为 0。

初始化 PerformanceEntry entry,给定 DOMHighResTimeStamp startTimeDOMString entryTypeDOMString name,以及可选 DOMHighResTimeStamp endTime(默认 0):

  1. 断言:entryType 已在 条目类型注册表中定义。
  2. entrystartTime 初始化为 startTime
  3. entryentryType 初始化为 entryType
  4. entryname 初始化为 name
  5. entry结束时间 初始化为 endTime

5. PerformanceObserver 接口

PerformanceObserver 接口可用于观察 性能时间线,以便在记录新性能指标时收到通知,并可选地接收已缓冲的性能指标。

每个 PerformanceObserver 具有以下关联概念:

PerformanceObserver(callback) 构造函数必须创建一个新的 PerformanceObserver 对象,其 观察者回调 设置为 callback,然后返回该对象。

已注册性能观察者 是一个 结构体 ,包含 观察者成员(一个 PerformanceObserver 对象)和 选项列表成员(一个 PerformanceObserverInit 字典列表)。

WebIDLcallback PerformanceObserverCallback = undefined (PerformanceObserverEntryList entries,
                                             PerformanceObserver observer,
                                             optional PerformanceObserverCallbackOptions options = {});
[Exposed=(Window,Worker)]
interface PerformanceObserver {
  constructor(PerformanceObserverCallback callback);
  undefined observe (optional PerformanceObserverInit options = {});
  undefined disconnect ();
  PerformanceEntryList takeRecords();
  [SameObject] static readonly attribute FrozenArray<DOMString> supportedEntryTypes;
};
注意

为了将性能开销降至最低,应用应只订阅自己感兴趣的事件类型,并在不再需要观察性能数据时断开观察者。按名称过滤不被支持,因为这会隐式要求订阅所有事件类型——虽然可以实现,但不推荐,因为这会产生大量事件。

5.1 PerformanceObserverCallbackOptions 字典

WebIDLdictionary PerformanceObserverCallbackOptions {
  unsigned long long droppedEntriesCount;
};
droppedEntriesCount
一个整数,表示观察者在 PerformanceObserver需要丢弃条目 被设置时,所观察条目类型的丢弃条目计数。

5.2 observe() 方法

observe() 方法指示用户代理注册观察者,必须执行以下步骤:

  1. relevantGlobalthis相关全局对象
  2. 如果 optionsentryTypestype 成员都未指定,则 抛出 "TypeError"。
  3. 如果 optionsentryTypes 存在且有其他成员同时存在,则 抛出 "TypeError"。
  4. 通过执行以下步骤,更新或检查 this观察者类型
    1. 如果 this观察者类型"undefined"
      1. 如果 optionsentryTypes 成员存在,则设置 this观察者类型"multiple"
      2. 如果 optionstype 成员存在,则 设置 this观察者类型"single"
    2. 如果 this观察者类型"single"optionsentryTypes 成员存在,则 抛出 "InvalidModificationError"。
    3. 如果 this观察者类型"multiple"optionstype 成员 存在,则 抛出 "InvalidModificationError"。
  5. this需要丢弃条目 设置为 true。
  6. 如果 this观察者类型"multiple",执行以下步骤:
    1. entry typesoptionsentryTypes 序列。
    2. 移除 entry types 中不包含在 relevantGlobal受支持条目类型的冻结数组中的所有类型。用户代理应当通知开发者 entry types 被修改。例如,可在控制台输出警告列出被移除的类型。
    3. 如果 entry types 结果序列为空,则终止这些步骤。用户代理应当在步骤被终止时通知开发者注册已中止。例如控制台警告。
    4. 如果 relevantGlobal已注册性能观察者对象列表 包含一个 已注册性能观察者,其 观察者this, 则用一个只包含 options 的列表替换其 选项列表
    5. 否则,创建并添加一个 已注册性能观察者对象到 relevantGlobal已注册性能观察者对象列表,其中 观察者设为 this选项列表设为一个只包含 options 的列表。
  7. 否则,执行以下步骤:
    1. 断言 this观察者类型"single"
    2. 如果 optionstype 不包含在 relevantGlobal受支持条目类型的冻结数组中,则终止这些步骤。用户代理应当通知开发者发生此情况,比如通过控制台警告。
    3. 如果 relevantGlobal已注册性能观察者对象列表 包含一个 已注册性能观察者 obs,其 观察者this
      1. 如果 obs选项列表包含一个 PerformanceObserverInitcurrentOptions ,其 type 等于 optionstype, 则用 options 替换 currentOptionsobs选项列表中。
      2. 否则,将 options 添加到 obs选项列表
    4. 否则,创建并添加一个 已注册性能观察者对象到 relevantGlobal已注册性能观察者对象列表,其中 观察者设为 this选项列表 设为一个只包含 options 的列表。
    5. 如果 optionsbuffered 标志被设置:
      1. tupleoptionstyperelevantGlobal相关性能条目元组
      2. tuple性能条目缓冲区中的每个 entry

        1. 如果 应添加条目,参数为 entryoptions,结果为 true,则 添加 entry观察者缓冲区
      3. 为 PerformanceObserver 排队任务, 参数为 relevantGlobal
注意

一个 PerformanceObserver 对象必须始终使用 optionsentryTypesoptionstype 调用 observe()。 如果一个 PerformanceObserver 同时以 entryTypestype 方式调用 observe,则会抛出异常。这是为避免调用堆叠的混乱。当使用 entryTypes 时,PerformanceObserverInit 的其他参数不能使用。此外, 多次 observe() 调用会覆盖旧值以保证兼容性,同时通常只需一次调用。另一方面, 使用 type 时,调用会堆叠,因为单次调用只能指定一个类型。重复调用 observe(),指定同一个 type,也会覆盖前者。

5.2.1 PerformanceObserverInit 字典

WebIDLdictionary PerformanceObserverInit {
  sequence<DOMString> entryTypes;
  DOMString type;
  boolean buffered;
};
entryTypes
要观察的条目类型列表。如果指定,列表不得为空,且其他成员不得同时存在。用户代理无法识别的类型必须被忽略。
type
要观察的单一条目类型。用户代理无法识别的类型必须被忽略。其他成员可同时存在。
buffered
是否将缓冲条目加入观察者缓冲区的标志。

5.2.2 PerformanceObserverEntryList 接口

WebIDL[Exposed=(Window,Worker)]
interface PerformanceObserverEntryList {
  PerformanceEntryList getEntries();
  PerformanceEntryList getEntriesByType (DOMString type);
  PerformanceEntryList getEntriesByName (DOMString name, optional DOMString type);
};

每个 PerformanceObserverEntryList 对象都关联一个 条目列表, 该列表由 PerformanceEntryList 组成,并在构造时初始化。

5.2.2.1 getEntries() 方法

返回一个 PerformanceEntryList 对象,由 按名称和类型过滤缓冲区 算法返回,参数为 this条目列表nametype 均为 null

5.2.2.2 getEntriesByType() 方法

返回一个 PerformanceEntryList 对象,由 按名称和类型过滤缓冲区 算法返回,参数为 this条目列表namenulltype 为方法输入的 type 参数。

5.2.2.3 getEntriesByName() 方法

返回一个 PerformanceEntryList 对象,由 按名称和类型过滤缓冲区 算法返回,参数为 this条目列表name 为方法输入的 name 参数,type 若省略可选参数 entryType 则为 null,否则为方法输入的 type 参数。

5.3 takeRecords() 方法

takeRecords() 方法必须返回 this观察者缓冲区 的副本,并同时清空 this观察者缓冲区

5.4 disconnect() 方法

disconnect() 方法必须执行以下操作:

  1. this 所属 已注册性能观察者对象列表中移除 this
  2. 清空 this观察者缓冲区
  3. 清空 this选项列表

5.5 supportedEntryTypes 属性

每个 全局对象都有一个相关的 受支持条目类型的冻结数组,其初始化为按照字母顺序排列、在注册表中针对该全局对象支持的字符串序列,通过 FrozenArray 创建

当调用 supportedEntryTypes 属性的 getter 时,执行以下步骤:

  1. globalObject环境设置对象的全局对象
  2. 返回 globalObject受支持条目类型的冻结数组
注意

该属性允许 Web 开发者方便地了解用户代理支持哪些条目类型。

6. 处理流程

6.1 队列 PerformanceEntry

队列 PerformanceEntry (newEntry),执行以下步骤:

  1. 如果 newEntryid 未设置:
    1. id 为对 newEntry 运行 生成 id 的结果。
    2. newEntryid 设为 id
  2. interested observers 为一个初始为空的 PerformanceObserver 对象集合。
  3. entryTypenewEntryentryType 值。
  4. relevantGlobalnewEntry相关全局对象
  5. 如果 relevantGlobal关联文档
    1. newEntrynavigationId 设为 relevantGlobal关联文档最近一次导航id
  6. 否则,将 newEntrynavigationId 设为 null。
  7. 对于 relevantGlobal已注册性能观察者对象列表中的每个 已注册性能观察者 regObs
    1. 如果 regObs选项列表包含一个 PerformanceObserverInit options,其 entryTypes 成员包含 entryType 或其 type 成员等于 entryType

      1. 如果 应添加条目,参数为 newEntryoptions,结果为 true,则将 regObs观察者 添加到 interested observers
  8. interested observers 中每个 observer
    1. newEntry 添加到 observer观察者缓冲区
  9. tupleentryTyperelevantGlobal相关性能条目元组
  10. isBufferFull判断性能条目缓冲区是否已满算法的结果,输入为 tuple
  11. shouldAdd应添加条目,输入为 newEntry 的结果。
  12. 如果 isBufferFull 为 false 且 shouldAdd 为 true,则将 newEntry 添加到 tuple性能条目缓冲区
  13. relevantGlobal 作为输入,为 PerformanceObserver 排队任务

6.2 队列导航 PerformanceEntry

队列导航 PerformanceEntry (newEntry),执行以下步骤:

  1. id 为对 newEntry 运行 生成 id 的结果。
  2. relevantGlobalnewEntry相关全局对象
  3. newEntryid 设为 id
  4. newEntrynavigationId 设为 id
  5. 如果 relevantGlobal关联文档
    1. relevantGlobal关联文档最近一次导航设为 newEntry
  6. newEntry 作为输入,队列 PerformanceEntry

6.3 为 PerformanceObserver 排队任务

当要求为 PerformanceObserver 排队任务时,输入为 relevantGlobal,执行以下步骤:

  1. 如果 relevantGlobal性能观察者任务已排队标志已设置,则终止这些步骤。
  2. 设置 relevantGlobal性能观察者任务已排队标志
  3. 队列任务,包括以下子步骤。 此队列任务的 任务来源性能时间线任务来源
    1. 取消设置 relevantGlobal性能观察者任务已排队标志
    2. notifyListrelevantGlobal已注册性能观察者对象列表 的副本。
    3. notifyList 中每个 已注册性能观察者对象 registeredObserver,执行以下步骤:
      1. poregisteredObserver观察者
      2. entriespo观察者缓冲区 的副本。
      3. 如果 entries 为空,则返回。
      4. 清空 po观察者缓冲区
      5. observerEntryList 为一个新的 PerformanceObserverEntryList, 其 条目列表 设为 entries
      6. droppedEntriesCount 为 null。
      7. 如果 po需要丢弃条目 已设置,执行以下步骤:
        1. droppedEntriesCount 设为 0。
        2. registeredObserver选项列表中的每个 PerformanceObserverInit item
          1. itemtypeentryTypes 中的每个 DOMString entryType
            1. maprelevantGlobal性能条目缓冲区映射
            2. tuple 为在 map 上以 entryType 获取条目值 的结果。
            3. droppedEntriesCount 增加 tuple丢弃条目计数
        3. po需要丢弃条目 设为 false。
      8. callbackOptions 为一个 PerformanceObserverCallbackOptions, 如果 droppedEntriesCount 非 null,则将其 droppedEntriesCount 设为 droppedEntriesCount,否则为未设置。
      9. 调用 po观察者回调,参数为 « observerEntryList, po, callbackOptions », "report", 和 po

性能时间线 任务队列是一个低优先级队列,用户代理应尽量在空闲时处理,以最大限度降低性能监控代码的影响。

6.4 按名称和类型过滤缓冲区映射

当要求执行 按名称和类型过滤缓冲区映射 算法,输入为可选 nametype 时,执行以下步骤:

  1. result 为初始为空的 列表
  2. map 为与 this性能条目缓冲区映射相关的 相关全局对象
  3. tuple list 为一个空 列表
  4. 如果 type 不为 null,则将 maptype 获取条目值 的结果添加到 tuple list。否则,将 map 获取所有值 的结果赋值给 tuple list
  5. tuple list 中每个 tuple,执行以下步骤:
    1. buffertuple性能条目缓冲区
    2. 如果 tupleavailableFromTimeline 为 false,则继续下一个 tuple
    3. entries 为对 buffernametype 执行 按名称和类型过滤缓冲区算法的结果。
    4. entries 中每个 entry添加 entryresult
  6. startTimeresults 的条目进行时间顺序排序
  7. 返回 result

6.5 按名称和类型过滤缓冲区

当要求运行 按名称和类型过滤缓冲区 算法,输入为 buffernametype,执行以下步骤:

  1. result 为初始为空的 列表
  2. buffer 中每个 PerformanceEntry entry,执行以下步骤:
    1. 如果 type 不为 null 且 type等同于 entryentryType 属性,则继续下一个 entry
    2. 如果 name 不为 null 且 name等同于 entryname 属性,则继续下一个 entry
    3. 添加 entryresult
  3. startTimeresults 的条目进行时间顺序排序
  4. 返回 result

6.6 判断性能条目缓冲区是否已满

判断性能条目缓冲区是否已满, 输入为 tuple,执行以下步骤:

  1. num current entriestuple性能条目缓冲区 的大小。
  2. 如果 num current entries 小于 tuplesmaxBufferSize,返回 false。
  3. tuple丢弃条目计数 增加 1。
  4. 返回 true。

6.7 生成 Performance Entry id

当要求为 PerformanceEntry entry 生成 id 时,执行以下步骤:

  1. relevantGlobalentry相关全局对象
  2. relevantGlobal最后性能条目 id 增加一个由用户代理选择的小数值。
  3. 返回 relevantGlobal最后性能条目 id

用户代理可以选择每次将 最后性能条目 id 增加一个小的随机整数。用户代理不得选择一个全局随机整数并将所有全局对象的 最后性能条目 id 增加该值,因为这可能导致跨域泄漏。

注意

最后性能条目 id 初始为随机值,且每次增加一个由用户代理选择的小数值,而不是 1,以避免开发者将其视为已生成条目数量的计数器。

7. 隐私注意事项

本规范扩展了 [HR-TIME-3] 定义的 Performance 接口,并提供了方法用于在 性能时间线 中队列和检索条目。有关高精度时间信息暴露的隐私注意事项,请参阅 [HR-TIME-3]。每个新增性能条目的规范都应有自己的隐私注意事项。

最后性能条目 id 故意初始化为随机值,并在每次队列新的 PerformanceEntry 时增加一个小值。用户代理可以选择对所有用户统一递增,也可以为每个 全局对象选择不同的递增,或者每个 PerformanceEntry 随机选择递增值。然而,为防止跨域信息泄露和指纹识别,用户代理不得为所有 PerformanceEntry 对象选择唯一的随机整数并用于所有 全局对象的递增。

8. 安全性注意事项

本规范扩展了 [HR-TIME-3] 定义的 Performance 接口,并提供了方法用于在 性能时间线 中队列和检索条目。有关高精度时间信息暴露的安全性注意事项,请参阅 [HR-TIME-3]。每个新增性能条目的规范都应有自己的安全性注意事项。

9. 依赖项

[INFRA] 规范定义了以下内容:key获取条目值

A. IDL 索引

WebIDLpartial interface Performance {
  PerformanceEntryList getEntries ();
  PerformanceEntryList getEntriesByType (DOMString type);
  PerformanceEntryList getEntriesByName (DOMString name, optional DOMString type);
};
typedef sequence<PerformanceEntry> PerformanceEntryList;

[Exposed=(Window,Worker)]
interface PerformanceEntry {
  readonly    attribute unsigned long long  id;
  readonly    attribute DOMString           name;
  readonly    attribute DOMString           entryType;
  readonly    attribute DOMHighResTimeStamp startTime;
  readonly    attribute DOMHighResTimeStamp duration;
  readonly    attribute unsigned long long  navigationId;
  [Default] object toJSON();
};

callback PerformanceObserverCallback = undefined (PerformanceObserverEntryList entries,
                                             PerformanceObserver observer,
                                             optional PerformanceObserverCallbackOptions options = {});
[Exposed=(Window,Worker)]
interface PerformanceObserver {
  constructor(PerformanceObserverCallback callback);
  undefined observe (optional PerformanceObserverInit options = {});
  undefined disconnect ();
  PerformanceEntryList takeRecords();
  [SameObject] static readonly attribute FrozenArray<DOMString> supportedEntryTypes;
};

dictionary PerformanceObserverCallbackOptions {
  unsigned long long droppedEntriesCount;
};

dictionary PerformanceObserverInit {
  sequence<DOMString> entryTypes;
  DOMString type;
  boolean buffered;
};

[Exposed=(Window,Worker)]
interface PerformanceObserverEntryList {
  PerformanceEntryList getEntries();
  PerformanceEntryList getEntriesByType (DOMString type);
  PerformanceEntryList getEntriesByName (DOMString name, optional DOMString type);
};

B. 致谢

感谢 Arvind Jain、Boris Zbarsky、Jatinder Mann、Nat Duca、Philippe Le Hegaret、Ryosuke Niwa、Shubhie Panicker、Todd Reifsteck、Yoav Weiss 以及 Zhiheng Wang 对本工作做出的贡献。

C. 参考文献

C.1 规范性引用

[dom]
DOM 标准。Anne van Kesteren。WHATWG。Living Standard。URL: https://dom.spec.whatwg.org/
[HR-TIME-3]
高精度时间。Yoav Weiss。W3C。2024年11月7日。W3C 工作草案。URL: https://www.w3.org/TR/hr-time-3/
[HTML]
HTML 标准。Anne van Kesteren;Domenic Denicola;Dominic Farolino;Ian Hickson;Philip Jägenstedt;Simon Pieters。WHATWG。Living Standard。URL: https://html.spec.whatwg.org/multipage/
[INFRA]
Infra 标准。Anne van Kesteren;Domenic Denicola。WHATWG。Living Standard。URL: https://infra.spec.whatwg.org/
[RFC2119]
用于 RFC 中指示需求级别的关键字。S. Bradner。IETF。1997年3月。最佳当前实践。URL: https://www.rfc-editor.org/rfc/rfc2119
[RFC8174]
RFC 2119 关键字中大小写歧义。B. Leiba。IETF。2017年5月。最佳当前实践。URL: https://www.rfc-editor.org/rfc/rfc8174
[WebIDL]
Web IDL 标准。Edgar Chen;Timothy Gu。WHATWG。Living Standard。URL: https://webidl.spec.whatwg.org/

C.2 参考性引用

[NAVIGATION-TIMING-2]
导航计时第二版。Yoav Weiss;Noam Rosenthal。W3C。2025年2月13日。W3C 工作草案。URL: https://www.w3.org/TR/navigation-timing-2/
[PERFORMANCE-TIMELINE]
性能时间线。Nicolas Pena Moreno。W3C。2025年2月13日。CRD。URL: https://www.w3.org/TR/performance-timeline/
[RESOURCE-TIMING-2]
资源计时。Yoav Weiss;Noam Rosenthal。W3C。2025年2月13日。CRD。URL: https://www.w3.org/TR/resource-timing/
[USER-TIMING-2]
用户计时第二版。Ilya Grigorik。W3C。2019年2月26日。W3C 推荐。URL: https://www.w3.org/TR/user-timing-2/
[WORKERS]
Web Workers。Ian Hickson。W3C。2021年1月28日。W3C 工作组说明。URL: https://www.w3.org/TR/workers/