performance.timing 是浏览器提供的原生性能监控 API,用于精确测量页面加载各阶段耗时。对前端开发者而言,它是诊断首屏性能瓶颈、优化用户体验的重要工具。
一、API 结构:performance.timing 对象
performance.timing 是一个只读对象,包含页面加载过程中 15+ 个时间戳(单位:毫秒,自 Unix 纪元)。
核心字段(按时间顺序):
| 字段 | 含义 | 说明 |
|---|---|---|
| navigationStart | 导航开始时间 | 所有时间的基准(用户点击链接/回车) |
| domainLookupStart | DNS 查询开始 | |
| domainLookupEnd | DNS 查询结束 | DNS 耗时 = domainLookupEnd - domainLookupStart |
| connectStart | TCP 连接开始 | |
| connectEnd | TCP 连接结束 | TCP 耗时 = connectEnd - connectStart |
| requestStart | HTTP 请求开始 | |
| responseStart | HTTP 响应首字节 | TTFB(Time To First Byte) = responseStart - navigationStart |
| responseEnd | HTTP 响应结束 | |
| domLoading | DOM 解析开始 | |
| domInteractive | DOM 就绪(可交互) | 关键:用户可操作时间 |
| domContentLoadedEventEnd | DOMContentLoaded 事件结束 | JS 执行完毕 |
| loadEventEnd | load 事件结束 | 完整页面加载完成 |
📌 注意:performance.timing已废弃(但所有浏览器仍支持);现代替代:performance.getEntriesByType('navigation')[0](Navigation Timing API Level 2)。
二、首屏时间(First Screen Time)的计算
'首屏时间'无标准定义,但通常指用户看到主要内容的时间。常用三种指标:
1. FP(First Paint)
- 浏览器首次渲染像素(非白屏);
- 不包括:默认背景色;
- 计算:需用 PerformancePaintTiming(见下文)。
2. FCP(First Contentful Paint)
- 首次渲染文本、图片、Canvas 等内容;
- 行业标准(Google Core Web Vitals);
:

