Created
May 21, 2013 12:27
-
-
Save fedir/5619422 to your computer and use it in GitHub Desktop.
Detect internet speed in Javascript // ref. : http://jsfiddle.net/yahavbr/tEFpC/2/ http://stackoverflow.com/questions/5529718/how-to-detect-internet-speed-in-javascript
This file contains 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
var imageAddr = "http://www.tranquilmusic.ca/images/cats/Cat2.JPG" + "?n=" + Math.random(); | |
var startTime, endTime; | |
var downloadSize = 5616998; | |
var download = new Image(); | |
download.onload = function () { | |
endTime = (new Date()).getTime(); | |
showResults(); | |
} | |
startTime = (new Date()).getTime(); | |
download.src = imageAddr; | |
function showResults() { | |
var duration = (endTime - startTime) / 1000; //Math.round() | |
var bitsLoaded = downloadSize * 8; | |
var speedBps = (bitsLoaded / duration).toFixed(2); | |
var speedKbps = (speedBps / 1024).toFixed(2); | |
var speedMbps = (speedKbps / 1024).toFixed(2); | |
alert("Your connection speed is: \n" + | |
speedBps + " bps\n" + | |
speedKbps + " kbps\n" + | |
speedMbps + " Mbps\n" ); | |
} |
For what it's worth, Mozilla still reports Chrome (and by my testing Opera) caps the downlink speed to 10Mb/s to avoid fingerprinting. What a shame web advertisers have ruined specificity on an otherwise helpful feature to determine download speed. In general, the downlink feature can still be used to determine a disadvantaged client (slow downlink) vs a speedy client.
The image/website no longer exists, and therefore this code is broken. It needs to be replaced with an image of the same size.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It still an experimental feature, so the script above seem a legit workaround, thanks!