Skip to content

Instantly share code, notes, and snippets.

@sgelob
Created August 29, 2018 08:33
Show Gist options
  • Save sgelob/b4f324fca06452a44e2099d7a8c644c1 to your computer and use it in GitHub Desktop.
Save sgelob/b4f324fca06452a44e2099d7a8c644c1 to your computer and use it in GitHub Desktop.
(function() {
/**
* Creates array of timing entries from Resource Timing Interface
*/
function getTimings() {
var entries = [];
var resources = [];
if (window.performance.getEntriesByType !== undefined) {
resources = window.performance.getEntriesByType("resource");
} else if (window.performance.webkitGetEntriesByType !== undefined) {
resources = window.performance.webkitGetEntriesByType("resource");
}
for (var n = 0; n < resources.length; n++) {
entries.push(createEntryFromResourceTiming(resources[n]));
}
return entries;
}
/**
* Creates an entry from a PerformanceResourceTiming object
*/
function createEntryFromResourceTiming(resource) {
return {
url: resource.name,
start: resource.startTime,
duration: resource.duration,
redirectStart: resource.redirectStart,
redirectDuration: resource.redirectEnd - resource.redirectStart,
appCacheStart: 0, // TODO
appCacheDuration: 0, // TODO
dnsStart: resource.domainLookupStart,
dnsDuration: resource.domainLookupEnd - resource.domainLookupStart,
tcpStart: resource.connectStart,
tcpDuration: (resource.secureConnectionStart > 0 ? resource.secureConnectionStart : resource.connectEnd) - resource.connectStart,
sslStart: resource.secureConnectionStart > 0 ? resource.secureConnectionStart : 0,
sslDuration: resource.secureConnectionStart > 0 ? resource.connectEnd - resource.secureConnectionStart : 0,
requestStart: resource.requestStart,
requestDuration: resource.responseStart - resource.requestStart,
responseStart: resource.responseStart,
responseDuration: resource.responseStart == 0 ? 0 : resource.responseEnd - resource.responseStart
};
}
// push.dataLayer
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment