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

Revision 4 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
Haversine
var dLat = toRadian((to.lat - from.lat));
var dLon = toRadian((to.lon - from.lon));
var fromLat = toRadian(from.lat);
var toLat = toRadian(to.lat);

var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(fromLat) * Math.cos(toLat); 

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = EARTH_RADIUS * c;
ready
Spherical Law of Cosines
var st = Math.sin(toRadian(to.lat));
var ct = Math.sqrt( 1 - st*st );
var cf = Math.cos(toRadian(from.lat));
var d = (Math.PI/2 - toRadian(from.lat)) * st + cf * ct * Math.cos(toRadian(to.lon - from.lon))) * EARTH_RADIUS;
ready
Equirectangular approximation
var dLat = toRadian((to.lat - from.lat));
var dLon = toRadian((to.lon - from.lon));

var x = (dLon) * Math.cos(toRadian((from.lat + to.lat)/2));
var d = Math.sqrt(x*x + dLat*dLat) * EARTH_RADIUS;
ready

Revisions

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