@@ -39,23 +39,23 @@ This snippet is part of [pagespeed](https://github.com/corewebvitals/pagespeed)
39
39
40
40
// timing start times are relative
41
41
const activationStart = pageNav .activationStart || 0 ;
42
- const workerStart = Math .max (pageNav .workerStart - activationStart, activationStart );
43
- const dnsStart = Math .max (pageNav .domainLookupStart - activationStart, workerStart );
44
- const tcpStart = Math .max (pageNav .connectStart - activationStart, dnsStart );
45
- const sslStart = Math .max (pageNav .secureConnectionStart - activationStart, tcpStart );
46
- const requestStart = Math .max (pageNav .requestStart - activationStart, sslStart );
47
- const responseStart = Math .max (pageNav .responseStart - activationStart, requestStart );
42
+ const waitEnd = Math .max (( pageNav .workerStart || pageNav . fetchStart ) - activationStart, 0 );
43
+ const dnsStart = Math .max (pageNav .domainLookupStart - activationStart, 0 );
44
+ const tcpStart = Math .max (pageNav .connectStart - activationStart, 0 );
45
+ const sslStart = Math .max (pageNav .secureConnectionStart - activationStart, 0 );
46
+ const tcpEnd = Math .max (pageNav .connectEnd - activationStart, 0 );
47
+ const responseStart = Math .max (pageNav .responseStart - activationStart, 0 );
48
48
49
49
// attribution based on https://www.w3.org/TR/navigation-timing-2/#processing-model
50
50
// use associative array to log the results more readable
51
51
let attributionArray = [];
52
- attributionArray[' Redirect Time ' ] = {' time in ms' : formatTime (workerStart - activationStart )};
53
- attributionArray[' Worker and Cache Time ' ] = {' time in ms' : formatTime (dnsStart - workerStart )};
54
- attributionArray[' DNS Time ' ] = {' time in ms' : formatTime (tcpStart - dnsStart)};
55
- attributionArray[' TCP Time ' ] = {' time in ms' : formatTime (sslStart - tcpStart)};
56
- attributionArray[' SSL Time ' ] = {' time in ms' : formatTime (requestStart - sslStart)};
57
- attributionArray[' Request Time ' ] = {' time in ms' : formatTime (responseStart - requestStart )};
58
- attributionArray[' Total TTFB' ] = {' time in ms' : formatTime (responseStart - activationStart )};
52
+ attributionArray[' Redirect and Waiting duration ' ] = {' time in ms' : formatTime (waitEnd )};
53
+ attributionArray[' Worker and Cache duration ' ] = {' time in ms' : formatTime (dnsStart - waitEnd )};
54
+ attributionArray[' DNS duration ' ] = {' time in ms' : formatTime (tcpStart - dnsStart)};
55
+ attributionArray[' TCP duration ' ] = {' time in ms' : formatTime (sslStart - tcpStart)};
56
+ attributionArray[' SSL duration ' ] = {' time in ms' : formatTime (tcpEnd - sslStart)};
57
+ attributionArray[' Request duration ' ] = {' time in ms' : formatTime (responseStart - tcpEnd )};
58
+ attributionArray[' Total TTFB' ] = {' time in ms' : formatTime (responseStart)};
59
59
60
60
// log the results
61
61
console .log (' %cTime to First Byte (' + formatTime (responseStart - activationStart)+ ' ms)' , ' color: blue; font-weight: bold;' );
@@ -71,7 +71,6 @@ This snippet is part of [pagespeed](https://github.com/corewebvitals/pagespeed)
71
71
})();
72
72
```
73
73
74
-
75
74
## Measure the time to first byte of all the resources loaded
76
75
77
76
#### Snippet
0 commit comments