Skip to content

Instantly share code, notes, and snippets.

@debloper
Last active November 5, 2023 19:15
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save debloper/7296289 to your computer and use it in GitHub Desktop.
Save debloper/7296289 to your computer and use it in GitHub Desktop.
Determine client's connection speed with JavaScript
// Let's initialize the primitives
var startTime, endTime, fileSize;
// Set up the AJAX to perform
var xhr = new XMLHttpRequest();
// Rig the call-back... THE important part
xhr.onreadystatechange = function () {
// we only need to know when the request has completed
if (xhr.readyState === 4 && xhr.status === 200) {
// Here we stop the timer & register end time
endTime = (new Date()).getTime();
// Also, calculate the file-size which has transferred
fileSize = xhr.responseText.length;
// N.B: fileSize reports number of Bytes
// Calculate the connection-speed
var speed = fileSize / ((endTime - startTime)/1000) / 1024;
// Use (fileSize * 8) instead of fileSize for kBps instead of kbps
// Report the result, or have fries with it...
console.log(speed + " kbps\n");
}
}
// Snap back; here's where we start the timer
startTime = (new Date()).getTime();
// All set, let's hit it!
xhr.open("GET", "URL/TO/PROBE.FILE", true);
xhr.send();
@Sinstrite
Copy link

Sinstrite commented Feb 16, 2019

What are you calculating the connection speed in, Kibibits, Kilobits, etc?

A bit confused by the formula.

@debloper
Copy link
Author

debloper commented Jun 2, 2019

What are you calculating the connection speed in, Kibibits, Kilobits, etc?

A bit confused by the formula.

Check line #23

@ffilopeter
Copy link

What are you calculating the connection speed in, Kibibits, Kilobits, etc?
A bit confused by the formula.

Check line #23

Your units are wrong. KBps is kilobytes per second, but it should be kbps - kilobits per second
fileSize is in Bytes, because length property is number of characters, where each character is 8 bits long = 1 Byte
your formula is:
speed = (fileSize * 8) / ((endTime - startTime)/1000) / 1024
speed = (size in bits) / (time in seconds) / 1024
so it's kilobits/s = kbps, not kilobytes/s = kBps

@debloper
Copy link
Author

debloper commented Oct 3, 2019

@ffilopeter you're correct. I've just updated it. Thanks!

@celsobessa
Copy link

@ffilopeter you're correct. I've just updated it. Thanks!

Thanks for the gist. Just a quick heads up: seems like there still is some mix up with the units on lines 22 and 25. Unless I am wrong, it should read like this.

`
// Use (fileSize * 8) instead of fileSize for kbps instead of kBps

// Report the result, or have fries with it...
console.log(speed + " kbps\n");

`

@debloper
Copy link
Author

@celsobessa I must have had a brain-fart the last time I tried to "fix" it.
Thanks for pointing it out. How does it look now?

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