chrome.loadTimes()
是一个非标准 API,它向开发者公开加载指标和网络信息,以帮助他们更好地了解其网站的实际性能。
自 2009 年实现此 API 以来,您可以在标准化 API(例如以下 API)中找到它报告的所有实用信息:
- Navigation Timing 2
- 绘制时间
- 在 Navigation Timing 2 和 Resource Timing 2 中添加了
nextHopProtocol
。
这些标准化 API 目前由多个浏览器供应商实施。因此,Chrome 64 中已弃用 chrome.loadTimes()
。
已弃用的 API
chrome.loadTimes()
函数会返回一个包含其所有加载和网络信息的对象。例如,以下对象是在 www.google.com 上调用 chrome.loadTimes()
的结果:
{
"requestTime": 1513186741.847,
"startLoadTime": 1513186741.847,
"commitLoadTime": 1513186742.637,
"finishDocumentLoadTime": 1513186742.842,
"finishLoadTime": 1513186743.582,
"firstPaintTime": 1513186742.829,
"firstPaintAfterLoadTime": 0,
"navigationType": "Reload",
"wasFetchedViaSpdy": true,
"wasNpnNegotiated": true,
"npnNegotiatedProtocol": "h2",
"wasAlternateProtocolAvailable": false,
"connectionInfo": "h2"
}
标准替换项
现在,您可以使用标准化 API 找到上述每个值。下表将每个值与其标准化 API 进行了匹配,以下部分则提供了代码示例,展示了如何使用新版等效项获取旧版 API 中的每个值。
chrome.loadTimes() 功能
| 标准化 API 替换 |
---|---|
requestTime |
Navigation Timing 2 |
startLoadTime |
Navigation Timing 2 |
commitLoadTime |
Navigation Timing 2 |
finishDocumentLoadTime |
Navigation Timing 2 |
finishLoadTime |
导航计时 2 |
firstPaintTime |
绘制时间 |
firstPaintAfterLoadTime |
不适用 |
navigationType |
Navigation Timing 2 |
wasFetchedViaSpdy |
导航计时 2 |
wasNpnNegotiated |
Navigation Timing 2 |
npnNegotiatedProtocol |
导航计时 2 |
wasAlternateProtocolAvailable |
不适用 |
connectionInfo |
Navigation Timing 2 |
以下代码示例返回的值与 chrome.loadTimes()
返回的值等效。不过,对于新代码,不推荐使用这些代码示例。原因在于,chrome.loadTimes()
以秒表示的时间值,而新的性能 API 通常会以毫秒为单位报告与页面时间源相关的值,这往往对性能分析更有用。
其中一些示例还倾向于使用 Performance Timeline 2 API(例如 performance.getEntriesByType()
),但为旧版 Navigation Timing 1 API 提供了回退,因为该 API 的浏览器支持范围更广。今后,建议使用性能时间轴 API,并且通常报告的精度更高。
requestTime
function requestTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.startTime + performance.timeOrigin) / 1000;
} else {
return performance.timing.navigationStart / 1000;
}
}
startLoadTime
function startLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.startTime + performance.timeOrigin) / 1000;
} else {
return performance.timing.navigationStart / 1000;
}
}
commitLoadTime
function commitLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.responseStart + performance.timeOrigin) / 1000;
} else {
return performance.timing.responseStart / 1000;
}
}
finishDocumentLoadTime
function finishDocumentLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.domContentLoadedEventEnd + performance.timeOrigin) / 1000;
} else {
return performance.timing.domContentLoadedEventEnd / 1000;
}
}
finishLoadTime
function finishLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.loadEventEnd + performance.timeOrigin) / 1000;
} else {
return performance.timing.loadEventEnd / 1000;
}
}
firstPaintTime
function firstPaintTime() {
if (window.PerformancePaintTiming) {
const fpEntry = performance.getEntriesByType('paint')[0];
return (fpEntry.startTime + performance.timeOrigin) / 1000;
}
}
firstPaintAfterLoadTime
function firstPaintTimeAfterLoad() {
// This was never actually implemented and always returns 0.
return 0;
}
navigationType
function navigationType() {
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ntEntry.type;
}
}
wasFetchedViaSpdy
function wasFetchedViaSpdy() {
// SPDY is deprecated in favor of HTTP/2, but this implementation returns
// true for HTTP/2 or HTTP2+QUIC/39 as well.
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ['h2', 'hq'].includes(ntEntry.nextHopProtocol);
}
}
wasNpnNegotiated
function wasNpnNegotiated() {
// NPN is deprecated in favor of ALPN, but this implementation returns true
// for HTTP/2 or HTTP2+QUIC/39 requests negotiated via ALPN.
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ['h2', 'hq'].includes(ntEntry.nextHopProtocol);
}
}
npnNegotiatedProtocol
function npnNegotiatedProtocol() {
// NPN is deprecated in favor of ALPN, but this implementation returns the
// HTTP/2 or HTTP2+QUIC/39 requests negotiated via ALPN.
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ['h2', 'hq'].includes(ntEntry.nextHopProtocol) ?
ntEntry.nextHopProtocol : 'unknown';
}
}
wasAlternateProtocolAvailable
function wasAlternateProtocolAvailable() {
// The Alternate-Protocol header is deprecated in favor of Alt-Svc
// (https://www.mnot.net/blog/2016/03/09/alt-svc), so technically this
// should always return false.
return false;
}
connectionInfo
function connectionInfo() {
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ntEntry.nextHopProtocol;
}
}
移除计划
chrome.loadTimes()
API 将在 Chrome 64 中废弃,并计划于 2018 年底移除。开发者应尽快迁移其代码,以免数据丢失。