To detect the network speed using javascript, we will use the following approach.
Approach: Open the web page for which you want to know the connection speed. The page should be the one for which you want to add the javascript code for detecting the speed. Assign or set up the address of the image which you want to use for speed test to the variable. The variables for storing the test’s start time, end time, and download size should be created. Set the “downloadSize” equivalent to the image file size(In bytes). The end of the download action is assigned to activate when the image downloading is completed. It calculates the speed of the download process, and converts it to “kbps” and “mbps”.
Below is example illustrate the above approach:
Example:
<!DOCTYPE html> <html> <head> <title> To detect network speed using JavaScript </title> </head> <body> <script type="text/javascript"> var userImageLink = var time_start, end_time; // The size in bytes var downloadSize = 5616998; var downloadImgSrc = new Image(); downloadImgSrc.onload = function () { end_time = new Date().getTime(); displaySpeed(); }; time_start = new Date().getTime(); downloadImgSrc.src = userImageLink; document.write("time start: " + time_start); document.write("<br>"); function displaySpeed() { var timeDuration = (end_time - time_start) / 1000; var loadedBits = downloadSize * 8; /* Converts a number into string using toFixed(2) rounding to 2 */ var bps = (loadedBits / timeDuration).toFixed(2); var speedInKbps = (bps / 1024).toFixed(2); var speedInMbps = (speedInKbps / 1024).toFixed(2); alert("Your internet connection speed is: \n" + bps + " bps\n" + speedInKbps + " kbps\n" + speedInMbps + " Mbps\n"); } </script> </body> </html> |
Output:

Recommended Posts:
- Design a typing speed test game using JavaScript
- How to Configure Mouse Wheel Speed Across Browsers using JavaScript ?
- Detect a device is iOS or not using JavaScript
- How to detect touch screen device using JavaScript?
- Detect the Operating System of User using JavaScript
- How to detect flash is installed or not using JavaScript ?
- How to detect the device is an Android device using JavaScript ?
- How to detect operating system on the client machine using JavaScript ?
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- How to detect the Internet connection is offline or not using JavaScript?
- How to detect when the window size is resized using JavaScript ?
- How to detect If textbox content has changed using JavaScript ?
- How to detect whether a device is iOS without using Regular Expression in JavaScript?
- How to detect when cancel is clicked on file input using JavaScript ?
- How to detect page zoom level in all modern browsers using JavaScript ?
- How to Detect Keypress using JavaScript ?
- How to detect the browser language preference using JavaScript ?
- How to detect copy paste commands Ctrl+V, Ctrl+C using JavaScript ?
- How to play animation from start to end with same speed using CSS ?
- How to detect HTTP or HTTPS then force redirect to HTTPS in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


