Free javascript Hosting


perfanalytics.js

Uploaded on Dec 01 2021 01:44 by lefayev791

function perfAnalyticsData() {
let perfData = window.performance.timing;

let ttfb = perfData.responseStart - perfData.navigationStart;

let fcp = performance
.getEntriesByType("paint")
.filter((ppt) => ppt.name === "first-contentful-paint")[0].startTime;

var DOMloadTime =
perfData.domContentLoadedEventEnd - perfData.navigationStart;

var windowLoadTime = perfData.loadEventEnd - perfData.navigationStart;

var resourceEntries = performance.getEntriesByType("resource");

var scriptLoadTime = calcResourceDuration(resourceEntries.filter((prt) => prt.initiatorType === "script"));

var stylingLoadTime = calcResourceDuration(resourceEntries.filter((prt) => prt.initiatorType === "link"));

var imgsLoadTime = calcResourceDuration(resourceEntries.filter((prt) => prt.initiatorType === "img"));

var fontsLoadTime = calcResourceDuration(resourceEntries.filter((prt) => prt.name.indexOf("woff") > 0));

var documentsLoadTime = calcResourceDuration(resourceEntries.filter((prt) => prt.initiatorType === "iframe"));

//www.uglifyjs.net/

return {
performance: {
TTFB: ttfb,
FCP: fcp,
DomLoad: DOMloadTime,
WindowLoad: windowLoadTime,
},
networkTimings: {
Images: imgsLoadTime,
JS: scriptLoadTime,
CSS: stylingLoadTime,
Fonts: fontsLoadTime,
Document: documentsLoadTime,
},
timestamp: Date.now()
};
}

function calcResourceDuration(res){
if(res.length) {
return res.sort(function (a, b) {
return b.duration - a.duration;
})[0].duration;
} else return null;
}

Back to list