2026/4/11 15:32:21
网站建设
项目流程
做牛排的网站,做论坛网站怎么赚钱吗,网站下拉菜单html做多大,灯光设计师培训performance.timing 是浏览器提供的原生性能监控 API#xff0c;用于精确测量页面加载各阶段耗时。对前端开发者而言#xff0c;它是诊断首屏性能瓶颈、优化用户体验的黄金标准。 一、API 结构#xff1a;performance.timing 对象
performance.timing 是一个只读对象#…performance.timing是浏览器提供的原生性能监控 API用于精确测量页面加载各阶段耗时。对前端开发者而言它是诊断首屏性能瓶颈、优化用户体验的黄金标准。一、API 结构performance.timing对象performance.timing是一个只读对象包含页面加载过程中15 个时间戳单位毫秒自 Unix 纪元。核心字段按时间顺序字段含义说明navigationStart导航开始时间所有时间的基准用户点击链接/回车domainLookupStartDNS 查询开始domainLookupEndDNS 查询结束DNS 耗时 domainLookupEnd - domainLookupStartconnectStartTCP 连接开始connectEndTCP 连接结束TCP 耗时 connectEnd - connectStartrequestStartHTTP 请求开始responseStartHTTP 响应首字节TTFBTime To First ByteresponseStart - navigationStartresponseEndHTTP 响应结束domLoadingDOM 解析开始domInteractiveDOM 就绪可交互关键用户可操作时间domContentLoadedEventEndDOMContentLoaded 事件结束JS 执行完毕loadEventEndload 事件结束完整页面加载完成注意performance.timing已废弃但所有浏览器仍支持现代替代performance.getEntriesByType(navigation)[0]Navigation Timing API Level 2。二、首屏时间First Screen Time的计算“首屏时间”无标准定义但通常指用户看到主要内容的时间。常用三种指标1.FPFirst Paint浏览器首次渲染像素非白屏不包括默认背景色计算需用PerformancePaintTiming见下文。2.FCPFirst Contentful Paint首次渲染文本、图片、Canvas 等内容行业标准Google Core Web Vitals计算constfcpperformance.getEntriesByName(first-contentful-paint)[0];console.log(FCP:,fcp.startTime);3.TTITime to Interactive页面可交互元素可点击、输入计算复杂需第三方库如tti-polyfill。⚠️performance.timing无法直接获取 FCP/FP只能估算首屏 ≈domInteractiveDOM 解析完成CSSOM 就绪完全加载 loadEventEnd - navigationStart。三、用performance.timing估算关键指标consttperformance.timing;// 1. DNS 查询耗时constdnst.domainLookupEnd-t.domainLookupStart;// 2. TCP 连接耗时consttcpt.connectEnd-t.connectStart;// 3. TTFB后端响应速度constttfbt.responseStart-t.navigationStart;// 4. 内容传输耗时constcontentTransfert.responseEnd-t.responseStart;// 5. DOM 解析耗时constdomParset.domInteractive-t.responseEnd;// 6. 首屏时间估算constfirstScreent.domInteractive-t.navigationStart;// 7. 完全加载时间constfullLoadt.loadEventEnd-t.navigationStart;console.table({dns,tcp,ttfb,contentTransfer,domParse,firstScreen,fullLoad});典型输出单位ms指标耗时优化方向DNS20DNS 预解析link reldns-prefetchTCP50HTTP/2 连接复用TTFB300PHP 优化、OPcache、DB 查询DOM 解析200减少 HTML 体积、JS 异步加载四、实战用法前端性能监控1.上报性能数据window.addEventListener(load,(){consttperformance.timing;constfirstScreent.domInteractive-t.navigationStart;// 上报到监控系统fetch(/perf-log,{method:POST,body:JSON.stringify({firstScreen,url:location.href})});});2.诊断慢页面若TTFB 500ms→ 后端问题PHP/DB若DOM 解析 500ms→ 前端问题大 HTML、同步 JS若TCP 100ms→ 网络问题跨机房、无 CDN。3.与后端联动在 PHP 响应头添加Server-Timingheader(Server-Timing: db;dur150, app;dur50);前端通过performance.getEntriesByType(navigation)[0].serverTiming读取。五、现代替代方案Navigation Timing Level 2performance.timing已废弃推荐使用// 获取导航性能等效于 performance.timingconstnavperformance.getEntriesByType(navigation)[0];// 获取 FCP首屏内容渲染constfcpperformance.getEntriesByName(first-contentful-paint)[0];// 获取资源加载详情constresourcesperformance.getEntriesByType(resource);优势支持FCP、FP、LCP等 Core Web Vitals支持Resource Timing单个 JS/CSS 耗时支持Server-Timing后端自定义指标。六、PHP 程序员如何利用监控 TTFB若ttfb 300ms用xhprof/EXPLAIN优化 PHP/DB减少响应体积开启 Gzipob_start(ob_gzhandler)移除无用 HTML/JS利用 Server-Timing// 在 PHP 中标记关键阶段$startmicrotime(true);// ... DB 查询 ...$dbTime(microtime(true)-$start)*1000;header(Server-Timing: db;dur$dbTime);七、总结概念performance.timing现代方案核心价值测量页面加载各阶段耗时支持 Core Web Vitals首屏时间估算domInteractive精确FCP/LCPTTFB✅ 精确✅ 精确前端优化指导 HTML/JS 优化指导用户体验优化后端联动通过 TTFB 定位 PHP/DB 问题通过 Server-Timing 暴露内部耗时✅对 PHP 程序员的终极价值用performance.timing证明“慢”不是前端问题而是后端 TTFB 高。掌握此 API你就能用数据驱动前后端协作而非陷入“甩锅”争论。