Geohash vs Quadtree vs R-tree: benchmarking proximity search on 1 million points
October 29, 2026

Almost every system design interview with a map in it (Uber, Yelp, DoorDash, Tinder) reaches the same question: how do you quickly find the things near a user? The answer usually comes as a list of trade-offs. Geohash is simple, a quadtree adapts to density, an R-tree is what PostGIS uses. I wanted numbers, so I implemented the structures from Hello Interview's proximity search deep dive in C++ and measured each one on 1 million points.
The code is on GitHub: JoaoLeonello/proximity-search. It is header-only C++20 with no dependencies, and runs everything.
The problem
The query we want to answer is "every driver within 500 m of me". A regular index doesn't make that cheap. handles one dimension: the database finds the right latitude band and then has to look at every row in it to filter by longitude. In a city 50 km wide, a 1 km band still crosses the whole city from one side to the other.
Spatial indexes handle both dimensions at once. I tested four of them, plus a naive scan as the reference.
The structures
1. B-tree on latitude
This is what you get for free in a relational database. The implementation is a vector sorted by latitude with binary search, which is the leaf level of a B+tree. It is in the benchmark to show how far an index that isn't spatial can go.
2. Geohash
A geohash interleaves the bits of longitude and latitude into a single number (Morton order). Nearby points get nearby numbers, and every prefix is a contiguous range of the sorted array. For a radius search you pick the precision at which a cell is at least as large as the radius, then scan the query's cell plus its 8 neighbours. The circle can never leave that 3×3 block, so the whole search is 9 binary searches. Redis works this way.
3. Quadtree
Each node covers a square. Once it holds more than 32 points, it splits into four. The tree ends up deep downtown, where there are lots of points, and shallow in the suburbs. To find the nearest neighbour it runs a best-first search: a priority queue always expands the node closest to the query and stops when no unvisited node can contain a point better than the ones already found.
4. R-tree
Each node stores the rectangle that encloses its children, and those rectangles may overlap. I used STR (Sort-Tile-Recursive) bulk loading: sort by x, cut into vertical slices, sort each slice by y, pack runs of 16, and repeat level by level. It indexes rectangles natively, so the same tree works for points and polygons. PostGIS's GiST index follows the same idea.
The experiment
The data sits in a 50 × 50 km box over greater São Paulo. 70% of the points cluster around 12 "downtowns" (Gaussian, about 2 km standard deviation) and 30% are spread uniformly. I did it this way because real location data is clustered, and clustering is exactly what separates adaptive trees from fixed grids.
There are six scenarios, 2,000 queries each:
500 m radius ("drivers near me")
5 km radius ("restaurants in my area")
Nearest neighbour, k = 1 ("closest driver")
The 20 nearest
Point in polygon ("which delivery zones cover this address?"), over 20,000 zones
Polygon overlap ("which zones does a new zone run into?")
Every answer from every structure is compared with the naive scan, query by query. If a single id is missing or extra, the benchmark fails.
Results
Average time per query in microseconds (Ryzen 5 5600GT, g++ 15, , single thread, best of 3 runs). Lower is better.
Scenario | Scan | B-tree | Geohash | Quadtree | R-tree |
|---|---|---|---|---|---|
500 m radius | 888 | 86.9 | 13.7 | 13.2 | 11.6 |
5 km radius | 1,552 | 1,034 | 656 | 622 | 453 |
k = 1 | 1,295 | 52.8 | 13.4 | 0.87 | 1.95 |
k = 20 | 1,295 | 59.8 | 21.2 | 3.34 | 4.55 |
Point in polygon | 65.0 | n/a | 3.67 | 9.49 | 4.49 |
Polygon overlap | 140 | n/a | 52.0 | 56.6 | 61.1 |
No structure wins everything. The quadtree's nearest neighbour lookup is 1,494 times faster than the scan, and for point in polygon the winner is the geohash.
Why each one wins where it does
Radius search: memory matters more than the algorithm
Once the index has found the right neighbourhood, what's left is reading candidates. The STR R-tree keeps 16 entries per leaf in one contiguous array, and leaves that are neighbours on the map are also neighbours in memory. The geohash has a similar property along its Z-curve, but its 9 cells cover at least 9r² of area for a circle of πr², so it reads at least 2.9 times more area than it needs.
With a 5 km radius each query returns 56,000 points, and every index turns into "copy 56,000 ids". The advantage over the scan drops to 2 or 3 times. In that case you need pagination or ranking, because no index makes the answer smaller.
Nearest neighbour: the trees pull away
The quadtree and the R-tree walk outward from the query and stop as soon as no remaining node can beat the current best result. A geohash or a B-tree has no idea which node is closest. They guess a radius, double it until k points fit, and pay for every attempt that came back short. The quadtree beat the R-tree here, and my guess (I haven't profiled it) is that each expansion pushes 4 children onto the priority queue instead of 16.
Polygons: the grid is hard to beat
For "which zones cover this point", the geohash does one hash map lookup and tests a short list of candidates. The R-tree descends about four levels through nodes that overlap. The polygon quadtree (MX-CIF) came last: a zone that crosses a split line is stored at that line's level, so every query re-tests the zones parked near the root.
For overlap the three are tied. Each query returns about 280 zones, and the exact intersection test (SAT) costs more than finding the candidates.
What surprised me: the "slow" quadtree
In the first run the quadtree came last on radius search, at 35 µs against the geohash's 13 µs. The algorithm was fine. Its leaves stored only point ids and fetched coordinates from the main array, which meant one random memory access per candidate. The geohash and the R-tree kept coordinates next to the id.
Once I moved the coordinates into the leaves, the quadtree dropped to 13.2 µs, 2.7 times faster, and tied with the geohash. My takeaway: in a data structure benchmark, comparing structures with different memory layouts ends up measuring the cache. I made all of them consistent on that point before publishing the numbers.
So which one should you use?
If you can only pick one, pick the R-tree. It is never far from first place, it indexes rectangles, and PostGIS already gives it to you.
Use a geohash when writes need to be cheap (drivers sending their location all the time), when you want to shard by prefix, or when Redis is already in your stack.
Use a quadtree for an in-memory "find the nearest" service over very uneven data.
A B-tree on latitude is 10 to 25 times faster than the scan on small queries and up to 60 times slower than the spatial indexes. It's fine to start with while the volume is small.
What's not covered
Every index is built once, and in the real world drivers move. With updates, the geohash (re-sort one key) and the quadtree (local insert and delete) should have an edge over a bulk-loaded R-tree. I also didn't test disk, distribution across machines, the whole globe (the planar metric assumes a single city), or structures like k-d trees, S2, and H3.
References
Hello Interview, Proximity Search deep dive.
Code and full results: github.com/JoaoLeonello/proximity-search.
Antonin Guttman, R-Trees: A Dynamic Index Structure for Spatial Searching (1984), the original R-tree paper.
Scott Leutenegger, Mario Lopez, and Jeffrey Edgington, STR: A Simple and Efficient Algorithm for R-Tree Packing (1997).
bitwiseopsonmaster❯logout