Last active
December 21, 2015 19:29
-
-
Save Tickthokk/6354615 to your computer and use it in GitHub Desktop.
Detect Page Speed with javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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