21

In the code below, I am trying to calculate the download speed of an image, but the speed comes out as infinity. What am I doing wrong?

var imageAddr = "/images/image.jpg" + "?n=" + Math.random();
var startTime, endTime;
var downloadSize = 200000;
var download = new Image();
download.onload = function () {
    endTime = (new Date()).getTime();
    showResults();
}
startTime = (new Date()).getTime();
download.src = imageAddr;

function showResults() {
    var duration = Math.round((endTime - startTime) / 1000);
    var bitsLoaded = downloadSize * 8;
    var speedBps = Math.round(bitsLoaded / duration);
    var speedKbps = (speedBps / 1024).toFixed(2);
    var speedMbps = (speedKbps / 1024).toFixed(2);
    alert("Your connection speed is: \n" + 
           speedBps + " bps\n"   + 
           speedKbps + " kbps\n" + 
           speedMbps + " Mbps\n" );
}
miyuru
  • 1,141
  • 11
  • 19
Rajeev
  • 44,985
  • 76
  • 186
  • 285

4 Answers4

9

Just don't round the duration.

 var duration = (endTime - startTime) / 1000;
Thai
  • 10,746
  • 2
  • 45
  • 57
  • I got the download speed as 13793103 bps,13469.83 kbps13.15 Mbps.This is incorrect as my internet connection is limited to 3Mbps.This should be 1.3 Mbps – Rajeev Jan 03 '11 at 10:35
  • First, you have to make sure that the image isn't sent gzipped, because that would make it a bit faster (not much, but inaccurate). Then, you need to make sure that you are having the right `downloadSize` number. Finally, make sure that the image file is not on `localhost`. – Thai Jan 03 '11 at 11:11
  • What is it that about the download size? – Rajeev Jan 03 '11 at 11:20
  • 1
    The size, in bytes, of the image that you used. More size it is, more waiting time, and more accuracy. – Thai Jan 03 '11 at 11:22
  • 1
    @Rajeev sounds like you're performing the test on the local server itself so you're measuring the network or hard disk speed. Host this code on some remote server then you'll get real results. – Shadow The GPT Wizard Jan 03 '11 at 14:34
  • @Thai:Can we deduce upload/download result from this. – Rajeev Jan 04 '11 at 05:23
  • You can, but that's not very accurate. You can instead use Speedtest.net or it's widgets because they have servers dedicated for speed testing. – Thai Jan 04 '11 at 05:41
  • Is this an accurate computation of the network speed? – KnowledgeSeeker Sep 03 '18 at 08:31
8

duration is probably coming out 0, and a positive number divided by zero yields the special value of positive infinity in JavaScript.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Thats what i guessed ,but final values speedBps , speedKbps, speedMbps all are are infinity and these are not divided by duration – Rajeev Jan 03 '11 at 09:40
  • 3
    `speedBps` is computed like this: `var speedBps = Math.round(bitsLoaded / duration);`. If `duration` is 0, then `speedBps` will be infinity. And since the other two speed variables are calculated from `speedBps`, they will also be infinity. – cdhowie Jan 03 '11 at 10:11
8

Just think about it: endTime and startTime are in [ms], so their difference is also in ms.

Example with an image loading for 300 ms:

Math.round((endTime - startTime) / 1000);
-> Math.round(300 / 1000);
-> Math.round(0.3);
-> 0

Leave Math.round out of the snippet.

And then as the others stated duration = 0 will lead to

speedBps = bitsLoaded / duration
-> speedBps = bitsLoaded / 0
-> speedBps = Infinity

But, please note that you can't get accurate results like this. There is latency, connection time, time to first byte, etc which cannot be measured by your example, and for an image < 1 MB they will lead to very inaccurate results.

gblazex
  • 49,155
  • 12
  • 98
  • 91
  • 1
    How to determine the upload/download limit from this – Rajeev Jan 03 '11 at 14:54
  • 1
    I had tested this method and several similar techniques with very little luck in getting meaningful results from the numbers. In my testing small medium and large files yielded wildly different rates of download. One would expect the similar rates - so I think rates is not an accurate measurement on which to reach a conclusion about speed..... – alQemist Sep 26 '14 at 21:07
3

because your duration is close to 0, then you should try

var duration = (endTime - startTime)/1000;

ilumin
  • 608
  • 12
  • 22