-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
timeToFirstBytesUtils.js
37 lines (33 loc) · 1.71 KB
/
timeToFirstBytesUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Calculates the Time to First Byte (TTFB) for the given window object.
*
* This function attempts to use the Navigation Timing Level 2 API first, and falls back to
* the Navigation Timing Level 1 API if the former is not available.
*
* @param {Window} win - The window object from which to retrieve performance timing information.
* @returns {string} The TTFB in milliseconds as a string, or an empty string if the TTFB cannot be determined.
*/
export function getTimeToFirstByte(win) {
const performance = win.performance || win.webkitPerformance || win.msPerformance || win.mozPerformance;
const ttfbWithTimingV2 = performance &&
typeof performance.getEntriesByType === 'function' &&
Object.prototype.toString.call(performance.getEntriesByType) === '[object Function]' &&
performance.getEntriesByType('navigation')[0] &&
performance.getEntriesByType('navigation')[0].responseStart &&
performance.getEntriesByType('navigation')[0].requestStart &&
performance.getEntriesByType('navigation')[0].responseStart > 0 &&
performance.getEntriesByType('navigation')[0].requestStart > 0 &&
Math.round(
performance.getEntriesByType('navigation')[0].responseStart - performance.getEntriesByType('navigation')[0].requestStart
);
if (ttfbWithTimingV2) {
return ttfbWithTimingV2.toString();
}
const ttfbWithTimingV1 = performance &&
performance.timing.responseStart &&
performance.timing.requestStart &&
performance.timing.responseStart > 0 &&
performance.timing.requestStart > 0 &&
performance.timing.responseStart - performance.timing.requestStart;
return ttfbWithTimingV1 ? ttfbWithTimingV1.toString() : '';
}