How to Detect Network Speed using JavaScript? Last Updated : 21 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Network speed detection in JavaScript involves measuring the time it takes to download a known file or resource and calculating the download speed. To calculate the speed of the network a file of known size is chosen from a server to download. The time taken to start and complete the download is recorded and using the file size and the time taken, the download speed is calculated.ApproachOpen 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 that you want to use for the speed test to the variable. The variables for storing the test's start time, end time, and download size should be created. Set the "download Size" 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". Example: Below is an example illustrating the above approach. HTML <!DOCTYPE html> <html> <head> <title> To detect network speed using JavaScript </title> </head> <body> <script type="text/javascript"> let userImageLink = "https://media.geeksforgeeks.org/wp-content/cdn-uploads/20200714180638/CIP_Launch-banner.png"; let time_start, end_time; // The size in bytes let downloadSize = 5616998; let downloadImgSrc = new Image(); downloadImgSrc.onload = function () { end_time = new Date().getTime(); displaySpeed(); }; time_start = new Date().getTime(); downloadImgSrc.src = userImageLink; function displaySpeed() { let timeDuration = (end_time - time_start) / 1000; let loadedBits = downloadSize * 8; /* Converts a number into string using toFixed(2) rounding to 2 */ let bps = (loadedBits / timeDuration).toFixed(2); let speedInKbps = (bps / 1024).toFixed(2); let 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: Comment More infoAdvertise with us Next Article How to detect when the window size is resized using JavaScript ? R romy421kumari Follow Improve Article Tags : JavaScript Web Technologies javascript-basics HTML-Misc JavaScript-Questions +1 More Similar Reads How to ping a server using JavaScript ? Pinging a server is used to determine whether it is online or not. The idea is to send an echo message to the server (called ping) and the server is expected to reply back with a similar message (called pong). Ping messages are sent and received by using ICMP (Internet Control Messaging Protocol). T 3 min read How to change video playing speed using JavaScript ? In this article, we will see how we can change the playback speed of videos embedded in an HTML document using an HTML5 video tag.We can set the new playing speed using the playbackRate attribute. It has the following syntax.Syntax:let video = document.querySelector('video')video.playbackRate = newP 1 min read How to detect the browser language preference using JavaScript ? Detecting the language preferences of users can be very important for Websites or Web Apps to increase user interaction. In JavaScript, this task can be easily done by using the Languages property available for the navigator interface. The navigator.language and the navigator.languages property toge 2 min read How to get Camera Resolution using JavaScript ? In this article, we will learn to find the maximum resolution supported by the camera. We need to request camera access from the user and once access is given we can check the resolution of the video stream and find out the resolution given by the camera. The .getUserMedia() method asks the user for 2 min read How to detect when the window size is resized using JavaScript ? Sometimes when we develop our site, we want to detect the size of the window, In this article, we are going to learn how to detect when the window size is resized using JavaScriptThe window resize event occurs whenever the size of the browser window gets changed. We can listen to the resize event in 3 min read How to detect when the window size is resized using JavaScript ? Sometimes when we develop our site, we want to detect the size of the window, In this article, we are going to learn how to detect when the window size is resized using JavaScriptThe window resize event occurs whenever the size of the browser window gets changed. We can listen to the resize event in 3 min read Like