Skip to content

Instantly share code, notes, and snippets.

@choonkending
Last active January 15, 2017 23:15
Show Gist options
  • Save choonkending/e21380a75bf02c08cdc206f1b0950e0a to your computer and use it in GitHub Desktop.
Save choonkending/e21380a75bf02c08cdc206f1b0950e0a to your computer and use it in GitHub Desktop.
Print navigation timing in your browser
const events = [ 'domLoading', 'domInteractive', 'domContentLoadedEventEnd', 'domComplete', 'loadEventEnd' ];
const startEvent = 'navigationStart';
const timing = performance.timing;
const compose = (f, g) => x => f(g(x));
const timeFromNavigationStart = event => differenceInSeconds(timing[event], timing[startEvent]);
const differenceInSeconds = (t1, t2) => (t1 - t2) / 1000;
const sanitizeTime = t => (t > 0) ? t : 'N/A - Has not fired yet - please run console log again';
const getTimeDifference = compose(sanitizeTime, timeFromNavigationStart);
const buildEventsArray = events => events.map((event) => {
const timeDifference = getTimeDifference(event);
return new Event(event, timeDifference);
});
class Event {
constructor(navigationTiming, seconds) {
this.navigationTiming = navigationTiming;
this.seconds = seconds;
}
}
console.table(buildEventsArray(events));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment