Skip to content

Instantly share code, notes, and snippets.

@BrettBukowski
Last active August 29, 2015 14:08
Show Gist options
  • Save BrettBukowski/db3f0513487dde1eba35 to your computer and use it in GitHub Desktop.
Save BrettBukowski/db3f0513487dde1eba35 to your computer and use it in GitHub Desktop.
When we can safely access all performance.timing properties
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>When We Can Safely Access all performance.timing properties</title>
<script>
debugger;
</script>
</head>
<body>
<main id="main"></main>
</body>
<script>
function timingReady () {
var out = document.querySelector('main');
var timing = window.performance.timing;
out.innerHTML += "<p>Total Response Time: " + (timing.responseEnd - timing.requestStart) + "ms</p>";
out.innerHTML += "<p>Time Until Page Loaded: " + (timing.loadEventEnd - timing.navigationStart) + "ms</p>";
}
if ('performance' in window && window.performance.timing) {
// <http://caniuse.com/#feat=nav-timing>
if (document.readyState == 'complete') {
// If this script is being included asynchronously later on then the load event has already fired.
timingReady();
}
else {
// The browsers that don't support the timing API also don't support #addEventListener (IE < 9).
window.addEventListener('load', function () {
// loadEventEnd isn't available until after the 'load' event is finished.
setTimeout(timingReady, 1);
});
}
}
else {
document.getElementById('main').innerHTML = 'browser not supported';
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment