Skip to content

Compute Euclidean and spherical distances. VERY FAST. TINY.

Notifications You must be signed in to change notification settings

wbrickner/NanoGeoUtil.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NanoGeoUtil.js

Compute Euclidean and spherical distances. VERY FAST (slowest function ~160 nanoseconds, meaning ~6.5M ops/sec). TINY (248 bytes gzipped).

Quick start

It's as easy as...

<script src="NanoGeoUtil.min.js"></script>

Documentation

Sphere Distance

The distance between two points on the surface of a sphere (the earth)

var distanceInMeters = NanoGeoUtil.sphereDistance(latitudeA, longitudeA, latitudeB, longitudeB);

Performance: 153.69ns = 6506604 op/s

Euclidean Distance

The distance between two points on a flat plane (note: a good approximation of the sphereDistance in small distances)

var distanceInUnits = NanoGeoUtil.euclidDistance(x1, y1, x2, y2);

Performance: 22.46ns = 44523597 op/s

Kilometers to miles

// 10km is 6.21371192mi, so miles will equal 6.21371192
var miles = NanoGeoUtil.kmToMi(10);

Performance: 17.19ns = 58173357 op/s

Miles to kilometers

// 10mi is 16.09344km, so kilometers will equal 16.09344
var kilometers = NanoGeoUtil.miToKm(10);

Performance: 12.50ns = 80000000 op/s

Performance Note

I've tested performance on my MacBook Pro (Retina, 13-inch, Late 2012) (2.5 GHz Intel Core i5; 8 GB 1600 MHz DDR3). Performance may be wildly different on your device. If you navigate to the HTML file in the performance_test directory, I'd be very interested to hear the results (open the developer console to view them).

License

No license, please do whatever you like with this code, personal or commercial project. I would love it if you could link back here or give me credit in the form of a small note somewhere, something like "Uses NanoGeoUtil by Will Brickner".

Good luck, have fun!