the GeolocationCoordinates object returned by Geolocation API already contains calculated speed property. So that before calculating anything by a custom code you can just check whether the speed was returned and if so, then just use it.
Although it is not mandatory for a browser to return it, on modern phones with modern browsers it is usually returned.
https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates/speed