forked from GoogleChrome/web-vitals
-
Notifications
You must be signed in to change notification settings - Fork 1
Snippets for LSN using PerformanceObserver
Michal Mocny edited this page Jan 26, 2021
·
2 revisions
{
let cls = 0;
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
if (!entry.hadRecentInput) {
cls += entry.value;
console.log('Current CLS value:', cls, entry);
}
}
}).observe({type: 'layout-shift', buffered: true});
}
{
let cls = 0, count = 0, prevTs = Number.NEGATIVE_INFINITY;
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
if (entry.hadRecentInput) continue;
if (entry.startTime - prevTs > 5000) count++;
prevTs = entry.startTime;
cls += entry.value;
console.log('Current avg-session-gap5s value:', cls/count, entry);
}
}).observe({type: 'layout-shift', buffered: true});
}
{
let max = 0, curr = 0, prevTs = Number.NEGATIVE_INFINITY;
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
if (entry.hadRecentInput) continue;
if (entry.startTime - prevTs > 1000) curr = 0;
prevTs = entry.startTime;
curr += entry.value;
max = Math.max(max, curr);
console.log('Current MAX-session-gap1s value:', max, entry);
}
}).observe({type: 'layout-shift', buffered: true});
}
{
let max = 0, curr = 0, firstTs = Number.NEGATIVE_INFINITY, prevTs = Number.NEGATIVE_INFINITY;
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
if (entry.hadRecentInput) continue;
if (entry.startTime - firstTs > 5000 || entry.startTime - prevTs > 1000) {
firstTs = entry.startTime;
curr = 0;
}
prevTs = entry.startTime;
curr += entry.value;
max = Math.max(max, curr);
console.log('Current MAX-session-gap1s-limit5s value:', max, entry);
}
}).observe({type: 'layout-shift', buffered: true});
}
{
let max = 0, curr = 0, entries = [];
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
if (entry.hadRecentInput) continue;
while (entries.length && entry.startTime - entries[0].startTime > 1000) curr -= entries.shift().value;
entries.push(entry);
curr += entry.value;
max = Math.max(max, curr);
console.log('Current MAX-sliding-1s value:', max, entry);
}
}).observe({type: 'layout-shift', buffered: true});
}
{
let max = 0, curr = 0, entries = [];
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries()) {
if (entry.hadRecentInput) continue;
while (entries.length && entry.startTime - entries[0].startTime > 300) curr -= entries.shift().value;
entries.push(entry);
curr += entry.value;
max = Math.max(max, curr);
console.log('Current MAX-sliding-300ms value:', max, entry);
}
}).observe({type: 'layout-shift', buffered: true});
}