Skip to content

Instantly share code, notes, and snippets.

@Tickthokk
Last active December 21, 2015 19:29
Show Gist options
  • Save Tickthokk/6354615 to your computer and use it in GitHub Desktop.
Save Tickthokk/6354615 to your computer and use it in GitHub Desktop.
Detect Page Speed with javascript
// Taken from https://github.com/guardian/frontend/blob/5f9c1dcbb2c4270fb356711b1091d3fff7844a72/common/app/assets/stylesheets/components/pasteup/js/modules/detect.js
function getPageSpeed() {
var start_time,
end_time,
total_time;
var perf = window.performance || window.msPerformance || window.webkitPerformance || window.mozPerformance;
if (perf && perf.timing) {
start_time = perf.timing.requestStart || perf.timing.fetchStart || perf.timing.navigationStart;
end_time = perf.timing.responseStart;
if (start_time && end_time) {
total_time = end_time - start_time;
}
}
return total_time;
}
function getConnectionSpeed() {
var load_time = getPageSpeed();
// Assume high speed for non supporting browsers.
var speed = "high";
if (load_time) {
if (load_time > 1000) { // One second
speed = 'medium';
if (load_time > 4000) { // Four seconds
speed = 'low';
}
}
}
return speed;
}
function getPixelRatio() {
return window.devicePixelRatio;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment