-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·77 lines (71 loc) · 2.88 KB
/
Copy pathapp.js
File metadata and controls
executable file
·77 lines (71 loc) · 2.88 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function perf(type, name, data, options="") {
//TODO: send the data to the server
console.log(`%c${type}: %c${name} | %c${data?Math.round(data)+'ms':''} %c${options}`, "color: red",
"color: green", "color: gray", "color: lightblue");
}
window.addEventListener("load", () => {
// Navigation Timing API
const navEntries = performance.getEntriesByType("navigation");
navEntries.forEach( entry => {
perf("navigation", "fetch-start", entry.fetchStart);
const ttfb = entry.responseStart-entry.fetchStart;
perf("navigation", "ttfb", ttfb);
});
if (navEntries.length==0) {
// old version
perf("navigation", "fetch-start", performance.timing.fetchStart);
const ttfb = performance.timing.responseStart-performance.timing.fetchStart;
perf("navigation", "ttfb", ttfb);
}
// Resource Timing API
const resEntries = performance.getEntriesByType("resource");
resEntries.forEach( entry => {
const size = `${Math.round(entry.encodedBodySize/1024)}Kb`;
const ttfb = entry.responseStart-entry.fetchStart;
perf(entry.initiatorType, entry.name, ttfb, size);
});
// Performance Observer for User Timing
const userObserver = new PerformanceObserver( list => {
list.getEntries().forEach( entry => {
perf(entry.entryType, entry.name,
entry.entryType=="mark" ? entry.startTime : entry.duration);
});
});
userObserver.observe({ entryTypes: ["mark", "measure"] });
// Paint Timing
const paintObserver = new PerformanceObserver( list => {
const firstPaint = list.getEntriesByName("first-paint");
if (firstPaint.length>0) {
perf("Paint", "First Paint", firstPaint[0].startTime);
}
const firstContentfulPaint = list.getEntriesByName("first-contentful-paint");
if (firstContentfulPaint.length>0) {
perf("Paint", "First Contentful Paint", firstContentfulPaint[0].startTime);
}
});
try {
userObserver.observe({ entryTypes: ["paint"] });
} catch (e) {
console.log("Paint Timing API not available");
}
// Long Task API
const taskObserver = new PerformanceObserver( list => {
list.getEntries().forEach( entry => perf("Long Task", "Thread used for too long"));
});
try {
taskObserver.observe({ entryTypes: ["longtask"]});
} catch (e) {}
// Frame Timing API
const frameObserver = new PerformanceObserver( list => {
list.getEntries().forEach( entry => perf("Frame drop", "Problem",
entry.duration));
});
try {
frameObserver.observe({ entryTypes: ["frame"]});
} catch (e) {}
// Server Timing
const serverTiming = performance.getEntriesByType("navigation")[0].serverTiming;
serverTiming.forEach( timing => {
perf("Server", timing.name, timing.duration);
})
});