Skip to content

Instantly share code, notes, and snippets.

@rictorres
Created July 18, 2013 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rictorres/6025860 to your computer and use it in GitHub Desktop.
Save rictorres/6025860 to your computer and use it in GitHub Desktop.
detect user bandwidth!
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" );
}
@rictorres
Copy link
Author

It's possible to some extent but won't be really accurate, the idea is load image with a known file size then in its onload event measure how much time passed until that event was triggered, and divide this time in the image file size.
Quick comparison with "real" speed test service showed small difference of 0.12 Mbps when using big picture.

http://stackoverflow.com/questions/5529718/how-to-detect-internet-speed-in-javascript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment