0

I have found really good example her stackoverflow how to detect internet speed with JavaScript. But when I run the code it shows 1.14 Mbps but in real speed test service i do get over 80 Mbps. Would any one tell me why is that and how can I improve this?

var imageAddr = "http://www.w3schools.com/css/trolltunga.jpg"; 
var downloadSize = 45941; //bytes

function ShowProgressMessage(msg) {
    if (console) {
        if (typeof msg == "string") {
            console.log(msg);
        } else {
            for (var i = 0; i < msg.length; i++) {
                console.log(msg[i]);
            }
        }
    }

    var oProgress = document.getElementById("progress");
    if (oProgress) {
        var actualHTML = (typeof msg == "string") ? msg : msg.join("<br />");
        oProgress.innerHTML = actualHTML;
    }
}

function InitiateSpeedDetection() {
    ShowProgressMessage("Testing your internet connection quality...");
    window.setTimeout(MeasureConnectionSpeed, 1);
};    

if (window.addEventListener) {
    window.addEventListener('load', InitiateSpeedDetection, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', InitiateSpeedDetection);
}

function MeasureConnectionSpeed() {
    var startTime, endTime;
    var download = new Image();
    download.onload = function () {
        endTime = (new Date()).getTime();
        showResults();
    }

    download.onerror = function (err, msg) {
        ShowProgressMessage("Invalid image, or error downloading");
    }

    startTime = (new Date()).getTime();
    var cacheBuster = "?nnn=" + startTime;
    download.src = imageAddr + cacheBuster;

    function showResults() {
        var duration = (endTime - startTime) / 1000;
        var bitsLoaded = downloadSize * 8;
        var speedBps = (bitsLoaded / duration).toFixed(2);
        var speedKbps = (speedBps / 1024).toFixed(2);
        var speedMbps = (speedKbps / 1024).toFixed(2);
        ShowProgressMessage([
            "Your connection speed is:", 
            speedBps + " bps", 
            speedKbps + " kbps", 
            speedMbps + " Mbps"
        ]);
    }
}
Itslearning
  • 47
  • 1
  • 6
  • Well, maybe the server on the other end only pushes at 1.14Mbps? – pawel Oct 28 '16 at 12:58
  • Maybe the w3schools server is a bit slow to serve images? – Bergi Oct 28 '16 at 12:59
  • I find it hard ot believe that the above didn't appear when you typed the title of this question? [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) *"Search, and research"* – Liam Oct 28 '16 at 13:02
  • I can't imagine that you would ever get an accurate speed using a 45K image. Download speed is dependent upon many factors and can change during a request, which is why better speed tests use a much larger resource (multiple MB) in order to get an average speed over a longer period of time. – Dan Wilson Oct 28 '16 at 13:07
  • #Liam yes this is a dublicate question. fact I have pointed from beginning I have found this her in stackoverflow. But there is no comment why there is differences. – Itslearning Oct 28 '16 at 13:07

1 Answers1

2

I think this is a problem with the behavior how packages are transfered in the web.

I cannot explain it 100% but the transmission start with a lower speed than possible and increases during transfer when possible. So you will never get the maximum speed when you are doing measuring that way. You can get near to maximum speed when you are downloading a bigger file (try 5mb) and you will get are more precise result.

user967165
  • 86
  • 3