How to Detect Network Speed using JavaScript? Last Updated : 21 Apr, 2025 Comments Improve Suggest changes 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 info R romy421kumari Follow Improve Article Tags : JavaScript Web Technologies javascript-basics HTML-Misc JavaScript-Questions +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like