1
1
# Time To First Byte
2
2
3
- ## Measure the time to first byte, from the document
3
+
4
+ Time to First Byte (TTFB) is a measurement used as an indication of the responsiveness of a webserver or other network resource.
5
+ TTFB measures the duration from the user or client making an HTTP request to the first byte of the page being received by the client's browser.
6
+
7
+ ## Measure the time to first byte, from the server
4
8
5
9
#### Snippet
6
10
@@ -16,6 +20,58 @@ new PerformanceObserver((entryList) => {
16
20
});
17
21
```
18
22
23
+ ## Mesure the TTFB sub-parts
24
+
25
+ This snippet is part of [ pagespeed] ( https://github.com/corewebvitals/pagespeed ) by [ Arjen Karel] ( https://www.linkedin.com/in/arjenkarel/ )
26
+
27
+ #### Snippet
28
+
29
+ ``` js copy
30
+ (() => {
31
+
32
+ const formatTime = (time ) => {
33
+ // round by 2 decimals, use Math.round() for integer
34
+ return Math .round (time * 100 ) / 100 ;
35
+ };
36
+
37
+ new PerformanceObserver ((entryList ) => {
38
+ const [pageNav ] = entryList .getEntriesByType (' navigation' );
39
+
40
+ // timing start times are relative
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);
48
+
49
+ // attribution based on https://www.w3.org/TR/navigation-timing-2/#processing-model
50
+ // use associative array to log the results more readable
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)};
59
+
60
+ // log the results
61
+ console .log (' %cTime to First Byte (' + formatTime (responseStart - activationStart)+ ' ms)' , ' color: blue; font-weight: bold;' );
62
+ console .table (attributionArray);
63
+
64
+ console .log (' %cOrigininal navigation entry' , ' color: blue; font-weight: bold;' );
65
+ console .log (pageNav);
66
+
67
+ }).observe ({
68
+ type: ' navigation' ,
69
+ buffered: true
70
+ });
71
+ })();
72
+ ```
73
+
74
+
19
75
## Measure the time to first byte of all the resources loaded
20
76
21
77
#### Snippet
0 commit comments