Internet Speed Test Using JavaScript

Internet Speed Test Using JavaScript
Code Snippet:Speed Test with Javascript
Author: Josh B
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 1,369
License: MIT
Edit Code online: View on CodePen
Read More

This code enables you to perform an internet speed test using JavaScript. When you click the “Start” button, it downloads an image from a specified URL and calculates your connection speed in real-time. It then displays your speed in either Bps, KBps, or MBps, providing you with a quick assessment of your internet connection’s performance. This code is helpful for evaluating your internet speed on various devices and browsers.

Additionally, it can be a valuable tool for website administrators to monitor and optimize their site’s performance. You can use this code on your website to offer visitors a quick and easy way to check their internet speed. It helps users gauge their connection speed, assisting them in troubleshooting slow internet issues.

How to Create Internet Speed Test Tool using JavaScript

1. First, load the Google Fonts (optional), Prefixfree JS, and jQuery by adding the following CDN links into the head tag of your web/app project.

<link rel='stylesheet' href='|Alef'>
<script src=""></script>
<script src='//'></script>

2. After that, create the HTML structure for your internet speed test tool. You can customize the design and layout to match your website’s style. Make sure to include the necessary HTML elements, such as buttons and result containers.

<div class="main">
  <div class="header">
    <h2>Speed Test with JavaScript</h2>
  <div class="content">
    <button id="starttest">Start</button>
  <div id="results">
  <div id="disclaimer">
    <p>We regret due the early publish of this alpha version, we're still not comatible with all browsers. Tested with Chrome (27) and Firefox (21). This is not a final version.</p>

3. Add the following CSS code to your project to style the basic interface of your speed test tool. You can modify the styles to fit your website’s design. The CSS code can be customized according to your needs.

  font-size: 12px;
  font-family: 'Offside'; 
  background-color: #E6F6D8;
  font-size: 4.5em;
  color: #555;
h2, h3{
  text-align: center;

  margin: 20px;
  text-align: center;
  height: 120px;
  background-color: #71D971;
  border: 0px;
  border-radius: 15px;
  width: 400px;
  height: 100px;
  color: white;
  font-size: 2em;
  margin: 20px auto;
  font-family: 'Offside'
  background-color: #40B440;
  -webkit-box-shadow:  5px 5px 10px 1px rgba(0, 0, 0, .5);
  box-shadow:  5px 5px 10px 1px rgba(0, 0, 0, .5);
  margin-top: 30px;
  font-size: .8em;
  color: gray;
  text-align: center;

4. Copy and paste the following JavaScript code into your project. It measures the time it takes to download an image and displays the speed in either Bps, KBps, or MBps.

  Para poder poner este script en tu server tienes que poner tambien un archivo en tu server y saber de que tamaño es para poner la informacion en imageAddr y downloadSize.

var imageAddr = "" + "?n=" + Math.random();
var startTime, endTime;
var downloadSize = 5616998; //5.36Mb
var download = new Image();
var roundedDecimals = 2;
var bytesInAKilobyte = 1024;

function showResults() {
  var duration = (endTime - startTime) / 1000;
  var bitsLoaded = downloadSize * 8;
  var speedBps = ( bitsLoaded / duration ).toFixed(roundedDecimals);
  var displaySpeed = speed(speedBps);
  var results = "<h3>Your connection speed is<h3><h2>" + displaySpeed.value + " " + displaySpeed.units + "</h2>"
      $('#results').fadeIn('fast', function(){
        $('#starttest').text('Thank You!');

function speed( bitsPerSecond ){
  var KBps = (bitsPerSecond / bytesInAKilobyte).toFixed(roundedDecimals);
  if ( KBps <= 1 ) return { value: bitsPerSecond, units: "Bps" };
  var MBps = (KBps / bytesInAKilobyte).toFixed(roundedDecimals);
  if ( MBps <= 1 ) return { value: KBps, units: "KBps" };
  else return { value: MBps, units: "MBps" };

$('#starttest').on('click', function(){
  $('#starttest').text('Wait, downloading...');
  $('#starttest').attr('disabled', 'disabled');
  download.onload = function () {
    endTime = (new Date()).getTime();
  startTime = (new Date()).getTime();
  download.src = imageAddr;

That’s all! hopefully, you have successfully created an Internet Speed Test tool using JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link