haversine_salvador (v15)

Revision 15 of this benchmark created on


Description

Different haversine formulas

Setup

function deg2rad(deg) {
      return deg * Math.PI / 180
    }
    var const_deg2rad = Math.PI / 180;

Test runner

Ready to run.

Testing in
TestOps/sec
1
function distance(lat1, lon1, lat2, lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2 - lat1);  // deg2rad below
  var dLon = deg2rad(lon2 - lon1);
  var a = 
     Math.sin(dLat/2) * Math.sin(dLat/2) + 
     Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
     Math.sin(dLon/2) * Math.sin(dLon/2);

  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c; // Distance in km
  return d;
}

distance(48, -122, 49, -121);
ready
2
function distance(lat1, lon1, lat2, lon2) {
  var R = 6371;
  var dLat = deg2rad(lat2 - lat1);
  var dLon = deg2rad(lon2 - lon1);
  var a = 
     0.5 - Math.cos(dLat)/2 + 
     Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
     (1 - Math.cos(dLon))/2;

  return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
}

distance(48, -122, 49, -121);
ready
3
function distance(lat1, lon1, lat2, lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = (lat2 - lat1) * const_deg2rad;  // deg2rad below
  var dLon = (lon2 - lon1) * const_deg2rad;
  var a = 
     0.5 - Math.cos(dLat)/2 + 
     Math.cos(lat1 * const_deg2rad) * Math.cos(lat2 * const_deg2rad) * 
     (1 - Math.cos(dLon))/2;

  return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
}

distance(48, -122, 49, -121);
ready
4
function distance(lat1, lon1, lat2, lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = (lat2 - lat1) * const_deg2rad;  // deg2rad below
  var dLon = (lon2 - lon1) * const_deg2rad;
  var a = 
     0.5 - Math.cos(dLat)/2 + 
     Math.cos(lat1 * const_deg2rad) * Math.cos(lat2 * const_deg2rad) * 
     (1 - Math.cos(dLon))/2;

  return R * 2 * Math.asin(Math.sqrt(a));
}

distance(48, -122, 49, -121);
ready
5
function distance(lat1, lon1, lat2, lon2) {
  var deg2rad = const_deg2rad;
  lat1 *= deg2rad;
  lon1 *= deg2rad;
  lat2 *= deg2rad;
  lon2 *= deg2rad;
  var diam = 12742; // Diameter of the earth in km (2 * 6371)
  var dLat = lat2 - lat1;
  var dLon = lon2 - lon1;
  var a = (
     (1 - Math.cos(dLat)) + 
     (1 - Math.cos(dLon)) * Math.cos(lat1) * Math.cos(lat2)
  ) / 2;

  return diam * Math.asin(Math.sqrt(a));
}

distance(48, -122, 49, -121);
ready
28
function distance(lat1, lon1, lat2, lon2) {
  var cos = Math.cos;
  lat1 *= const_deg2rad; //0.017453292519943295;
  lon1 *= const_deg2rad; //0.017453292519943295;
  lat2 *= const_deg2rad; //0.017453292519943295;
  lon2 *= const_deg2rad; //0.017453292519943295;
  var diam = 12742; // Diameter of the earth in km (2 * 6371)
  var dLat = lat2 - lat1;
  var dLon = lon2 - lon1;
  var a = (
     (1 - cos(dLat)) + 
     (1 - cos(dLon)) * cos(lat1) * cos(lat2)
  ) / 2;

  return diam * Math.asin(Math.sqrt(a));
}

distance(48, -122, 49, -121);
ready
Slight Mods
function haversine ( fromLat, fromLon, toLat, toLon ) {

    var PNinfo = [];
    var closest = {id: -1, d: 99999999999};

        var lat1 = toLat, lon1 = toLon;
        var lat2 = fromLat, lon2 = fromLon;
        var deg2rad = const_deg2rad;

        var R = 6371; // km radius of earth
        var φ1 = lat1 * deg2rad;
        var φ2 = lat2 * deg2rad;
        var Δφ = (lat2 - lat1) * deg2rad;
        var Δλ = (lon2 - lon1) * deg2rad;

        var a = Math.sin( Δφ / 2 ) * Math.sin( Δφ / 2 ) +
            Math.cos( φ1 ) * Math.cos( φ2 ) *
            Math.sin( Δλ / 2 ) * Math.sin( Δλ / 2 );
        var c = 2 * Math.atan2( Math.sqrt( a ), Math.sqrt( 1 - a ) );

        var d = R * c;
    
    return d;
    
};

haversine(48, -122, 49, -121);
ready
As Was
function haversine ( fromLat, fromLon, toLat, toLon ) {

    var PNinfo = [];
    var closest = {id: -1, d: 99999999999};

        var lat1 = toLat, lon1 = toLon;
        var lat2 = fromLat, lon2 = fromLon;
       

        var R = 6371; // km radius of earth
        var φ1 = lat1 * const_deg2rad;
        var φ2 = lat2 * const_deg2rad;
        var Δφ = (lat2 - lat1) * const_deg2rad;
        var Δλ = (lon2 - lon1) * const_deg2rad;

        var a = Math.sin( Δφ / 2 ) * Math.sin( Δφ / 2 ) +
            Math.cos( φ1 ) * Math.cos( φ2 ) *
            Math.sin( Δλ / 2 ) * Math.sin( Δλ / 2 );
        var c = 2 * Math.atan2( Math.sqrt( a ), Math.sqrt( 1 - a ) );

        var d = R * c;
    
    return d;
    
};

haversine(48, -122, 49, -121);
ready
6
function distance(lat1, lon1, lat2, lon2) {
  var deg2rad = const_deg2rad; //0.017453292519943295;
  var cos = Math.cos;
  lat1 *= deg2rad;
  lon1 *= deg2rad;
  lat2 *= deg2rad;
  lon2 *= deg2rad;
  var diam = 12742; // Diameter of the earth in km (2 * 6371)
  var dLat = lat2 - lat1;
  var dLon = lon2 - lon1;
  var a = (
     (1 - cos(dLat)) + 
     (1 - cos(dLon)) * cos(lat1) * cos(lat2)
  ) / 2;

  return diam * Math.asin(Math.sqrt(a));
}

distance(48, -122, 49, -121);
ready

Revisions

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