Skip to content

Instantly share code, notes, and snippets.

@weiland
Created May 19, 2015 13:49
Show Gist options
  • Save weiland/178de8f8dccf460c1aad to your computer and use it in GitHub Desktop.
Save weiland/178de8f8dccf460c1aad to your computer and use it in GitHub Desktop.
window.performance usable metrics
let performance = window.performance;
let perfSupport = !!performance;
/**
* Receive Serve, DomComplete and PageLoaded timings
* @returns {Object}
*/
export function laodTimes() {
if (!perfSupport) {
return;
}
let t = performance.timing;
let navi = performance.navigation;
return {
redirectCount: navi.redirectCount,
//loadType: navi.type, // 0:user action(typing, link), 1:reload, 2: history move
latency: t.responseEnd - t.fetchStart,
serverTime: t.responseEnd - t.requestStart,
domComplete: t.domComplete - t.responseEnd,
pageLoad: t.loadEventEnd - t.responseEnd
};
}
// This information could be stored using sendBeacon()
try {
navigator.sendBeacon('/analytics', JSON.stringify(laodTimes()));
} catch {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment