chrome.loadTimes()
là một API không chuẩn, cho thấy các chỉ số tải và
thông tin mạng cho nhà phát triển để giúp họ hiểu rõ hơn về
hiệu suất của trang web trong thế giới thực.
Do API này được triển khai vào năm 2009 nên tất cả thông tin hữu ích mà API này báo cáo đều có thể có trong các API được chuẩn hoá, chẳng hạn như:
- Thời gian điều hướng 2
- Thời gian tạo điểm ảnh
nextHopProtocol
bổ sung vào Thời gian điều hướng 2 và Thời gian tài nguyên 2.
Nhiều nhà cung cấp trình duyệt đang triển khai những API đã chuẩn hoá này. Là một
nên chrome.loadTimes()
sẽ không được dùng nữa trong Chrome 64.
API không dùng nữa
Hàm chrome.loadTimes()
trả về một đối tượng duy nhất chứa mọi
tải và thông tin mạng. Ví dụ: đối tượng sau đây là kết quả
trong số gọi chrome.loadTimes()
trên www.google.com:
{
"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"
}
Thay thế được tiêu chuẩn hóa
Giờ đây, bạn có thể tìm thấy từng giá trị nêu trên bằng các API đã chuẩn hoá. Nội dung sau đây bảng sẽ khớp từng giá trị với API đã chuẩn hoá và các phần dưới đây cho thấy ví dụ về mã về cách lấy từng giá trị trong API cũ bằng các mã tương đương hiện đại.
chrome.loadTimes() đối tượng
| Thay thế API được chuẩn hoá |
---|---|
requestTime |
Thời gian điều hướng 2 |
startLoadTime |
Thời gian điều hướng 2 |
commitLoadTime |
Thời gian điều hướng 2 |
finishDocumentLoadTime |
Thời gian điều hướng 2 |
finishLoadTime |
Thời gian điều hướng 2 |
firstPaintTime |
Thời gian tạo điểm ảnh |
firstPaintAfterLoadTime |
Không áp dụng |
navigationType |
Thời gian điều hướng 2 |
wasFetchedViaSpdy |
Thời gian điều hướng 2 |
wasNpnNegotiated |
Thời gian điều hướng 2 |
npnNegotiatedProtocol |
Thời gian điều hướng 2 |
wasAlternateProtocolAvailable |
Không áp dụng |
connectionInfo |
Thời gian điều hướng 2 |
Các ví dụ về mã dưới đây trả về giá trị tương đương với các giá trị được trả về bởi
chrome.loadTimes()
. Tuy nhiên, đối với mã mới, các mã ví dụ này không
được đề xuất. Lý do là chrome.loadTimes()
cung cấp các giá trị thời gian theo thời gian bắt đầu của hệ thống tính bằng giây trong khi các API hiệu suất mới
thường báo cáo các giá trị tính bằng mili giây so với
nguồn gốc thời gian, thường
sẽ hữu ích hơn khi phân tích hiệu suất.
Một số ví dụ cũng ưu tiên API Dòng thời gian hiệu suất 2 (ví dụ:
performance.getEntriesByType()
) nhưng cung cấp tính năng dự phòng cho những
API Navigation Timing 1 vì API này có
hỗ trợ trình duyệt rộng hơn. Từ nay trở đi, API Dòng thời gian hiệu suất sẽ được ưu tiên
và thường được báo cáo với độ chính xác cao hơn.
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;
}
}
Kế hoạch xoá
API chrome.loadTimes()
sẽ không được dùng trong Chrome 64 nữa và được nhắm đến
bị loại bỏ vào cuối năm 2018. Nhà phát triển nên di chuyển mã của mình càng sớm càng tốt
để tránh mất dữ liệu.
Ý định không còn được dùng nữa | Trình theo dõi trạng thái Chrome | Lỗi Chromium