Haversine vs Spherical Law of Cosines vs Equirectangular approximation (v10)

Revision 10 of this benchmark created on


Setup

var from = {
      lat : 40,
      lon : -70
    }, to = { 
      lat : 40.7419,
      lon : -73.9930
    }, EARTH_RADIUS = 6371000;
    
    function toRadian(deg) {
      return deg /180 * Math.PI;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Spherical Law of Cosines
var d = Math.acos(Math.sin(toRadian(from.lat)) * 
          Math.sin(toRadian(to.lat)) + Math.cos(toRadian(from.lat)) * 
          Math.cos(toRadian(to.lat)) * Math.cos(toRadian(to.lon - from.lon))) * EARTH_RADIUS;
ready
Spherical Law of Cosines - precompute
const fromLat = toRadian(from.lat);
const toLat = toRadian(to.lat);
const dLon = toRadian(to.lon - from.lon);

var d = Math.acos(Math.sin(fromLat) * 
          Math.sin(toLat) + Math.cos(fromLat) * 
          Math.cos(toLat) * Math.cos(dLon)) * EARTH_RADIUS;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.