# 性能 API

性能 API（Performance API）是一组用于测量 Web 应用性能的 Web 标准，提供导航、资源加载和渲染计时等浏览器内置指标，以及供开发者在浏览器性能时间线上记录自有测量数据的接口。<sup>[1](https://developer.mozilla.org/docs/Web/API/Performance_API)</sup>这些标准主要由 W3C Web 性能工作组制定，其使命是提供测量用户代理特性和 API 在应用性能各方面表现的方法。<sup>[2](https://www.w3.org/2010/webperf/)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心数据单元 | 每个指标都是一个 `PerformanceEntry`，包含 `name`、`duration`、`startTime` 和 `entryType`；`PerformanceResourceTiming` 等专用接口对其加以扩展。<sup>[1](https://developer.mozilla.org/docs/Web/API/Performance_API)</sup> |
| 运行位置 | `Performance` 接口在 `Window` 和 `Worker` 全局作用域中均可用；条目特定于各自的执行上下文。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/API/Performance)</sup> |
| 首选访问方式 | 以回调构造的 `PerformanceObserver` 是接收条目的推荐方式；通知在空闲时间异步投递，以尽量减少性能影响。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup> |
| 时间基准 | 高分辨率时间（High Resolution Time）提供单调递增、亚毫秒级的时间戳，不受系统时钟偏移影响；对外暴露的时钟被粗化至 100 微秒，跨源隔离上下文中为 5 微秒。<sup>[5](https://www.w3.org/TR/2021/WD-hr-time-3-20210316/)</sup> |
| 缓冲区上限 | 每个全局对象的条目缓冲区有上限：`"resource"` 为 250（可调整），`"longtask"` 为 200，`"paint"` 为 2，`"mark"`、`"measure"` 和 `"navigation"` 不设上限。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup> |
| 浏览器支持 | 自 2020 年 1 月起，`PerformanceObserver` 已在各浏览器中广泛可用（Chrome 52、Firefox 57、Safari 11）。<sup>[6](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver)</sup> |
| 与 Web Vitals 的关系 | 最大内容绘制（Largest Contentful Paint）和交互到下次绘制（Interaction to Next Paint）等核心 Web Vitals 指标建立在这些性能条目之上；2024 年 3 月，INP 取代首次输入延迟（First Input Delay）成为响应性核心 Web Vital。<sup>[7](https://developers.google.com/search/blog/2023/05/introducing-inp)</sup> |

## API 的结构

性能 API 并非单一规范，而是一组规范。高分辨率时间规范定义了 `Performance` 接口、`DOMHighResTimeStamp` 类型和 `performance.now()` 方法，后者返回单调递增、亚毫秒级分辨率且不受系统时钟调整或偏移影响的时间值。<sup>[5](https://www.w3.org/TR/2021/WD-hr-time-3-20210316/)</sup>性能时间线（Performance Timeline）规范在此基础上扩展，提供存储和检索高分辨率性能指标数据的方法，在 Web Worker 中暴露 `PerformanceEntry`，并新增 `PerformanceObserver` 接口。<sup>[8](https://w3c.github.io/performance-timeline/)</sup>

在此基础之上，各项独立规范定义了具体的条目类型。导航计时（Navigation Timing）涵盖导航至文档并初始加载文档的指标；资源计时（Resource Timing）涵盖获取的资源；用户计时（User Timing）让开发者放置自己的标记和测量；此外还有规范分别涵盖绘制计时、事件计时、长任务和布局偏移。<sup>[2](https://www.w3.org/2010/webperf/)</sup><sup> • </sup><sup>[8](https://w3c.github.io/performance-timeline/)</sup>

## 性能条目

单条记录的数据点就是一个性能条目，即 `PerformanceEntry` 的实例。每个条目都有一个 `entryType` 字符串，描述其记录的内容：`"navigation"` 表示页面加载指标，`"resource"` 表示获取的资源，`"paint"` 表示关键渲染时刻，`"longtask"` 表示持续 50 毫秒及以上的任务，`"event"` 表示事件处理延迟，`"mark"` 和 `"measure"` 表示开发者创建的时间戳和间隔，其他还包括 `"layout-shift"`、`"largest-contentful-paint"` 和 `"first-input"` 等。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>

特定条目类型通过子类添加类型专属数据。`"resource"` 条目由 `PerformanceResourceTiming` 表示，它增加了 DNS 查询时间戳等属性；`PerformanceNavigationTiming` 继承自 `PerformanceResourceTiming`。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>基础属性的含义取决于子类：标记条目的 `name` 是标记的名称，而资源条目的 `name` 是资源 URL。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>

大多数条目由浏览器自动记录，无需开发者采取任何操作。部分指标需要主动启用：元素计时（Element Timing）要求为被测元素添加 `elementtiming` 属性，用户计时要求显式调用 `performance.mark()` 和 `performance.measure()`，服务器计时（Server Timing）要求服务器发送 `Server-Timing` HTTP 头。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>

## 访问数据

检索条目有两种方式。`Performance` 接口提供 `getEntries()`、`getEntriesByType()` 和 `getEntriesByName()`，从缓冲区返回条目。首选方式是 `PerformanceObserver`，它以回调构造，在匹配的条目被记录时调用该回调；其 `observe()` 方法接受要监视的条目类型和一个 `buffered` 选项，后者用于获取观察开始之前已发生的条目。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>

规范工作给出了首选观察者方式的三个理由：`getEntries*` 方法总是返回时间线开始以来的所有相关条目，因此重复调用会返回必须过滤的重复项；观察者通知异步投递，浏览器可在空闲时间派发；而且并非所有条目类型都能与 `getEntries*` 方法配合使用。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>观察者还通过静态属性 `PerformanceObserver.supportedEntryTypes` 支持特性检测，该属性返回用户代理支持的 `entryType` 值。<sup>[6](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver)</sup>

由于缓冲区有限，获取大量资源（例如通过轮询）的应用可能需要管理缓冲区大小或及时消费条目，以免丢失数据。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>

## 计时规范

**资源计时。** `PerformanceResourceTiming` 接口测量获取的 http(s) 资源，包括 `XMLHttpRequest` 对象，`iframe`、`img`、`script`、`object`、`embed` 和 `link` 样式表等 HTML 元素，SVG 元素以及 `EventSource`。来自 HTTP 缓存的资源也包括在内，因网络错误而中止的获取同样包括，但只带有 `startTime`、`fetchStart`、`duration` 和 `responseEnd`。`data:` URI 图像不包括在内，因为条目仅针对 http(s) 资源上报。<sup>[9](https://www.w3.org/TR/resource-timing/)</sup>

**用户计时。** 该规范让开发者可以访问用于自有测量的高精度时间戳，通过 `performance.mark()` 记录时间戳，通过 `performance.measure()` 记录两点之间的间隔，并可选带 `detail` 字段存放应用特定的元数据。<sup>[10](https://www.w3.org/TR/user-timing/)</sup>

**服务器计时。** 作为性能时间线指标的一部分，服务器计时提供 API 访问服务器传达给用户代理的请求-响应周期性能指标。<sup>[2](https://www.w3.org/2010/webperf/)</sup>

## 隐私与安全

高分辨率计时是一把双刃剑。高分辨率时间规范指出，缓存攻击、统计指纹识别以及 [Spectre](https://www.edgepedia.org/zh/spectre-security-vulnerability) 等微架构攻击可以利用高分辨率计时数据来区分用户或泄露同进程数据。为限制这一点，用户代理会粗化对外暴露的时间戳：默认时间分辨率为 100 微秒，仅当上下文处于跨源隔离状态时才降至 5 微秒，浏览器还可添加抖动或限制 API 调用频率。<sup>[5](https://www.w3.org/TR/2021/WD-hr-time-3-20210316/)</sup>

跨源资源计时另行受到限制。默认情况下，跨源资源仅暴露有限的计时属性，其余属性上报为零；服务器可通过返回 `Timing-Allow-Origin` HTTP 响应头来选择更充分的披露，该头列出允许查看这些值的源。<sup>[11](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Timing-Allow-Origin)</sup><sup> • </sup><sup>[9](https://www.w3.org/TR/resource-timing/)</sup>

## 在 Web Vitals 测量中的应用

Google 核心 Web Vitals 背后的指标建立于浏览器的性能 API 之上，而非由这些 API 直接暴露。例如，[累积布局偏移](https://www.edgepedia.org/zh/cumulative-layout-shift)（Cumulative Layout Shift）使用布局不稳定性 API（Layout Instability API）实现，`web-vitals` [JavaScript](https://www.edgepedia.org/zh/javascript) 库（约 2 KB）封装了底层条目，使收集的数据符合各指标的方法论。<sup>[12](https://web.dev/articles/vitals-measurement-getting-started?hl=en)</sup>交互到下次绘制通过事件计时 API 的 `"event"` 条目测量，这些条目记录浏览器启动事件处理程序所花的时间以及处理程序运行的时间。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)</sup>2024 年 3 月，INP 取代首次输入延迟成为核心 Web Vitals 的响应性指标。<sup>[7](https://developers.google.com/search/blog/2023/05/introducing-inp)</sup>

以此方式收集的实测数据与实验室数据的差异是可预期的：仅加载页面的实验室工具只能捕获初始加载期间的布局偏移，而 INP 完全无法在实验室中测量，因为它需要真实用户交互，对此[总阻塞时间](https://www.edgepedia.org/zh/total-blocking-time)（Total Blocking Time）是推荐的实验室替代指标。<sup>[12](https://web.dev/articles/vitals-measurement-getting-started?hl=en)</sup>

## References

1. [Performance APIs, MDN Web Docs](https://developer.mozilla.org/docs/Web/API/Performance_API)
2. [Web Performance Working Group, W3C](https://www.w3.org/2010/webperf/)
3. [Performance, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Performance)
4. [Performance data, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Performance_data)
5. [High Resolution Time Level 3 (Working Draft), W3C](https://www.w3.org/TR/2021/WD-hr-time-3-20210316/)
6. [PerformanceObserver, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver)
7. [Introducing INP to Core Web Vitals, Google Search Central Blog](https://developers.google.com/search/blog/2023/05/introducing-inp)
8. [Performance Timeline, W3C](https://w3c.github.io/performance-timeline/)
9. [Resource Timing, W3C](https://www.w3.org/TR/resource-timing/)
10. [User Timing, W3C](https://www.w3.org/TR/user-timing/)
11. [Timing-Allow-Origin header, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Timing-Allow-Origin)
12. [Getting started with measuring Web Vitals, web.dev](https://web.dev/articles/vitals-measurement-getting-started?hl=en)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies › Web performance metrics

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/performance-api

English original: https://www.edgepedia.org/performance-api
