Resource Timing API可以获取和分析应用资源加载的详细网络计时数据, 应用程序可以
Resource Timing API为网络事件(如重定向的开始和结束事件, DNS查找的开始和结束事件, 请求开始, 响应开始和结束时间等)生成有高分辨率时间戳( high-resolution timestamps )的资源加载时间线, 并提供了资源大小和资源类型.
Resource Timing 属性返回高分辨率时间戳, 顾名思义, 代表了高分辨率的时间点. 高分辨率时间戳类型是
, 用双精度数字(double)表示, 它的值是一个离散的时间点或者两个离散时间点之间的时间差.
DOMHighResTimeStamp的单位是毫秒(ms), 并且应该可以准确到 5 微秒(µs). 但是, 如果浏览器无法以提供精确到5微秒的时间值(如软硬件限制), 则可以将该值表示为以毫秒为单位的精确到毫秒的时间。
Resource loading timestamps
An application can get timestamps for the various stages used to load a resource. The first property in the processing model is
startTime which returns the timestamp immediately before the resource loading process begins. The
fetchStart timestamps follows and redirect processing (if applicable) and preceeds DNS lookup. The next stages are
connectEnd which are the timestamps immediately before and after connecting to the server, respectively. The last three timestamps are, in order:
requestStart - the timestamp before the browser starts requesting the resource from the server;
responseStart - the timestamp after the browser receives the first byte of the response from the server; and
responseEnd - the timestamp after the browser receives the last byte of the resource. If the resource is loaded via a secure connection a
secureConnectionStart timestamp will be available between the connection start and end events.
When CORS is in effect, many of these values are returned as zero unless the server's access policy permits these values to be shared. This requires the server providing the resource to send the
Timing-Allow-Origin HTTP response header with a value specifying the origin or origins which are allowed to get the restricted timestamp values.
The properties which are returned as 0 by default when loading a resource form a domain other than the one of the web page itself:
interface also includes several network timing properties. The
redirectEnd properties return
timestamps for redirect start and end times, respectively. Likewise, the The
domainLookupEnd properties return
timestamps for DNS lookup start and end times, respectively.
This would be a nice place to have a diagram showing the relationships between these segments of the resource loading time.
PerformanceResourceTiming interface has three properties that can be used to obtain size data about a resource. The
transferSize property returns the size (in octets) of the fetched resource including the response header fields plus the response payload body.
encodedBodySize property returns the size (in octets) received from the fetch (HTTP or cache), of the payload body, before removing any applied content-codings.
decodedBodySize returns the size (in octets) received from the fetch (HTTP or cache) of the message body, after removing any applied content-codings.
nextHopProtocol property returns the network protocol used to fetch the resource.
initiatorType property returns the type of resource that initiated the performance entry such as "
css" for a CSS resource, "
xmlhttprequest" for an XMLHttpRequest and "
img" for an image (such as a JPEG).
The Resource Timing API includes two methods that extend the
Performance interface. The
clearResourceTimings() method removes all "
resource" type performance entries from the browser's resource performance entry buffer. The
setResourceTimingBufferSize() method sets the resource performance entry buffer size to the specified number of resource
As shown in the
PerformanceResourceTiming interface's Browser Compatibility table, most of these interfaces are broadly implemented by desktop browsers. However, note that some properties have little to no implementation so see each property's "Browser compatibility" section for more specific interoperability data.
To test your browser's support for these interfaces, run the
- Resource Timing Standard; W3C Editor's Draft
- CanIUse data
- Resource Timing practical tips; Steve Souders; 2014 August 21
- Measuring network performance with Resource Timing API; Ilya Grigorik; 2013 December 11
- A Primer for Web Performance Timing APIs; Xiaoqian Wu; W3C Editor's Draft