haversine (v26)

Revision 26 of this benchmark created by some on


Description

Different haversine formulas

Test created by http://stackoverflow.com/users/1090562/salvador-dali

Modified by http://stackoverflow.com/users/36866/some

I wanted to test the performance of the functions, excluding the compile times of the functions. Therefore I moved the declarations of the functions to the initialization code, and then just called the different functions in the tests. In Firefox it made a great improvement to ops/sec.

Setup

function deg2rad(deg) {
      return deg * Math.PI / 180
    }
    
    function distance_1(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;
    }
    
    function distance_2(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));
    }
    
    function distance_3(lat1, lon1, lat2, lon2) {
      var R = 6371; // Radius of the earth in km
      var dLat = (lat2 - lat1) * Math.PI / 180; // deg2rad below
      var dLon = (lon2 - lon1) * Math.PI / 180;
      var a =
        0.5 - Math.cos(dLat) / 2 +
        Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
        (1 - Math.cos(dLon)) / 2;
    
      return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    }
    
    
    function distance_4(lat1, lon1, lat2, lon2) {
      var R = 6371; // Radius of the earth in km
      var dLat = (lat2 - lat1) * Math.PI / 180; // deg2rad below
      var dLon = (lon2 - lon1) * Math.PI / 180;
      var a =
        0.5 - Math.cos(dLat) / 2 +
        Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
        (1 - Math.cos(dLon)) / 2;
    
      return R * 2 * Math.asin(Math.sqrt(a));
    }
    
    
    function distance_5(lat1, lon1, lat2, lon2) {
      var deg2rad = Math.PI / 180;
      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));
    }
    
    function distance_6(lat1, lon1, lat2, lon2) {
      var deg2rad = 0.017453292519943295; // === Math.PI / 180
      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));
    }
    
    function distance_7(lat1, lon1, lat2, lon2) {
      var deg2rad = 0.017453292519943295; // === Math.PI / 180
      var cos = Math.cos;
      lat1 *= deg2rad;
      lon1 *= deg2rad;
      lat2 *= deg2rad;
      lon2 *= deg2rad;
      var a = (
        (1 - cos(lat2 - lat1)) +
        (1 - cos(lon2 - lon1)) * cos(lat1) * cos(lat2)
      ) / 2;
    
      return 12742 * Math.asin(Math.sqrt(a)); // Diameter of the earth in km (2 * 6371)
    }
    
    
    function distance_8(lat1, lon1, lat2, lon2) {
      var
        cos = Math.cos,
        asin = Math.asin,
        sqrt = Math.sqrt;
      var p = 0.017453292519943295;    // Math.PI / 180
      var a = 0.5 - cos( (lat2 - lat1) * p)/2 + 
              cos(lat1 * p) * cos(lat2 * p) * 
              (1 - cos((lon2 - lon1) * p))/2;
    
      return 12742 * asin(sqrt(a)); // 2 * R; R = 6371 km
    }
    
    
    function distance_9(lat1, lon1, lat2, lon2) {
      var p = 0.017453292519943295;    // Math.PI / 180
      var
        cos = Math.cos,
        asin = Math.asin,
        sqrt = Math.sqrt;
      lat1 *= p;
      lon1 *= p;
      lat2 *= p;
      lon2 *= p;
    
      var a = 0.5 - cos( lat2 - lat1 )/2 + 
              cos(lat1 ) * cos(lat2 ) * 
              (1 - cos(lon2 - lon1))/2;
    
      return 12742 * asin(sqrt(a)); // 2 * R; R = 6371 km
    }
    if (!window.flagOnce) {
      window.flagOnce=true;
      console.log('1',distance_1(48, -122, 49, -121));
      console.log('2',distance_2(48, -122, 49, -121));
      console.log('3',distance_3(48, -122, 49, -121));
      console.log('4',distance_4(48, -122, 49, -121));
      console.log('5',distance_5(48, -122, 49, -121));
      console.log('6',distance_6(48, -122, 49, -121));
      console.log('7',distance_7(48, -122, 49, -121));
      console.log('8',distance_8(48, -122, 49, -121));
      console.log('9',distance_9(48, -122, 49, -121));
    }

Test runner

Ready to run.

Testing in
TestOps/sec
1
distance_1(48, -122, 49, -121);
ready
2
distance_2(48, -122, 49, -121);
ready
3
distance_3(48, -122, 49, -121);
ready
4
distance_4(48, -122, 49, -121);
ready
5
distance_5(48, -122, 49, -121);
ready
6
distance_6(48, -122, 49, -121);
ready
7
distance_7(48, -122, 49, -121);
ready
8
distance_8(48, -122, 49, -121);
ready
9
distance_9(48, -122, 49, -121);
ready

Revisions

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