答案:Performance Timeline API 提供浏览器网络请求及页面性能的详细时序数据,通过 PerformanceResourceTiming 可分析 DNS、TCP、TLS、TTFB 等阶段耗时,定位瓶颈;结合 navigation、paint、longtask 等类型,可全面监控页面加载、渲染、交互等性能指标;使用 PerformanceObserver 实时收集数据,按关键指标聚合分析,实现从感知到量化的性能优化。

在前端性能优化的战场上,要真正把控用户体验,精准测量浏览器网络请求的时序是核心。
Performance Timeline API
PerformanceResourceTiming
要深入分析 JavaScript 浏览器网络请求时序,我们主要依赖
window.performance
Performance Timeline API
performance.getEntriesByType('resource')// 获取所有资源请求的性能数据
const resourceEntries = performance.getEntriesByType('resource');
resourceEntries.forEach(entry => {
// 过滤掉非HTTP/HTTPS请求,或者根据需要只关注特定类型的请求
if (!entry.name.startsWith('http')) {
return;
}
console.log(`--- 请求: ${entry.name} ---`);
console.log(`开始时间 (fetchStart): ${entry.fetchStart.toFixed(2)}ms`);
console.log(`DNS 查询耗时: ${(entry.domainLookupEnd - entry.domainLookupStart).toFixed(2)}ms`);
console.log(`TCP 连接耗时: ${(entry.connectEnd - entry.connectStart).toFixed(2)}ms`);
if (entry.secureConnectionStart > 0) {
console.log(`TLS/SSL 握手耗时: ${(entry.connectEnd - entry.secureConnectionStart).toFixed(2)}ms`);
}
console.log(`请求发起至首字节时间 (TTFB): ${(entry.responseStart - entry.requestStart).toFixed(2)}ms`);
console.log(`内容下载耗时: ${(entry.responseEnd - entry.responseStart).toFixed(2)}ms`);
console.log(`总耗时: ${(entry.duration).toFixed(2)}ms`);
console.log(`----------------------------------`);
});
// 如果需要实时监控,可以使用 PerformanceObserver
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach(entry => {
if (entry.entryType === 'resource' && entry.name.startsWith('http')) {
// 在这里处理新的资源请求数据
console.log('新资源请求完成:', entry.name, entry.duration);
}
});
});
observer.observe({ type: 'resource', buffered: true });通过这段代码,我们能拿到每个资源的详细时间戳,然后通过简单的减法运算,就能算出各个阶段的耗时。这比在 DevTools 里手动查看要高效得多,尤其是在需要自动化收集和分析大量数据时。
当我们拿到
PerformanceResourceTiming
fetchStart
domainLookupStart
domainLookupEnd
connectStart
connectEnd
secureConnectionStart
connectEnd - secureConnectionStart
requestStart
responseStart
responseStart - requestStart
responseEnd
responseEnd - responseStart
duration
fetchStart
responseEnd
识别网络瓶颈,其实就是看这些时间段中哪个特别长。比如,我曾经遇到一个项目,发现很多图片加载的
domainLookupEnd - domainLookupStart
responseStart - requestStart
Performance Timeline 的强大之处远不止于网络请求。它是一个统一的接口,能让你从多个维度去衡量和优化前端性能。除了
resource
navigation
domContentLoadedEventStart
loadEventStart
paint
first-contentful-paint
largest-contentful-paint
longtask
longtask
event
layout-shift
layout-shift
在我看来,Performance Timeline 就像一个多功能的诊断仪,它不只看发动机(网络),也看车身(渲染)、看驾驶员操作(交互)。结合这些数据,我们能构建一个更全面的性能视图,而不是只盯着某个单一指标。
在实际项目中,高效地收集和分析 Performance Timeline 数据,这可不是简单地在控制台里跑几行代码就能搞定的。这涉及到数据量、实时性、以及如何从海量数据中提炼出有价值的信息。
首先,对于数据收集,我通常会倾向于使用
PerformanceObserver
getEntriesByType
PerformanceObserver
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach(entry => {
// 根据 entry.entryType 和 entry.name 进行分类处理
if (entry.entryType === 'resource') {
// 收集资源加载数据
sendToAnalytics({
type: 'resource_timing',
url: entry.name,
duration: entry.duration,
// ... 其他关键时间戳
});
} else if (entry.entryType === 'paint' && entry.name === 'first-contentful-paint') {
// 收集 FCP 数据
sendToAnalytics({
type: 'fcp',
time: entry.startTime,
});
}
// ... 其他类型的数据
});
});
observer.observe({ entryTypes: ['resource', 'paint', 'longtask', 'navigation'], buffered: true });
function sendToAnalytics(data) {
// 实际项目中,这里会将数据发送到后端或第三方 RUM 服务
console.log('发送到分析系统:', data);
// fetch('/api/performance-data', { method: 'POST', body: JSON.stringify(data) });
}数据收集上来后,关键在于分析。我的经验是,不要试图把所有数据都存下来,那会是天文数字。我们应该关注关键指标,并对数据进行聚合。例如,对于资源请求,我们可以只记录加载时间最长的 N 个资源,或者对同域名下的资源进行平均耗时统计。对于 FCP、LCP 这些核心指标,可以直接上报其值。
在数据分析阶段,我会考虑以下几点:
这个过程,其实就是从“我知道页面慢”到“我知道具体哪个请求在哪个环节慢了,以及影响了多少用户”的转变。它要求我们不仅会写代码,还要懂一点数据分析和统计学。这才是真正把性能优化从玄学变成科学的路径。
以上就是JS 浏览器网络请求分析 - 使用 Performance Timeline 测量请求时序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号