Please tell me how to use JS to determine the speed of the Internet?

(I tried the following method, but sometimes it calculates incorrectly:, I note the current time (t1), take a picture, load it, then watch the time again (t2), calculate the difference (t2-t1) and then calculate the speed in kbit / s )

  • 2
    may show speed incorrectly, as the picture is cached. - KoVadim
  • if I'm not mistaken - js can't do it with anything - Grundy
  • To remove caching, you can request a picture with some thread GET parameter, and so, your method is working as much as possible for js - ThisMan
  • one
    I would open the console of the browser and see how the browser looks at your anti-cache. - KoVadim
  • 2
    it is necessary not to upload a picture, but something more weighty, then there will be a minimum of errors. - Jean-Claude

2 answers 2

There is no standard way yet. There is a connection api but it still works only for some mobile browsers.

So it remains to load any volume object and take the load time, which you are already doing. But there are problems here - if you use small objects, then there will be a big error in measurements, if there are large, and the user has a bad Internet, you will wait for a long time for the result, and the user will have a lot of traffic. You can also load a lot of small images in parallel, counting the average speed and stopping when the measurement error is quite small.

But it all makes sense if you consider the speed of the user connection. If you measure it in order to give it relevant content (of higher or lower quality), it is better to measure it in fact, and switch according to the situation (for the user, the speed may change at any time).

    1. Disable caching.
    2. Disable compression.

    Further under the scheme from a question.

    you will wait a long time for the result

    The server can break the connection if necessary.
    Or just pass chunked and finish when it sees fit.