Skip to content

Commit e012d81

Browse files
committed
Add TTFB Sub-parts snippet
1 parent 080be73 commit e012d81

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

pages/Loading/TTFB.mdx

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Time To First Byte
22

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
48

59
#### Snippet
610

@@ -16,6 +20,58 @@ new PerformanceObserver((entryList) => {
1620
});
1721
```
1822

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+
1975
## Measure the time to first byte of all the resources loaded
2076

2177
#### Snippet

0 commit comments

Comments
 (0)