Tiling Trauma
Welcome to the third installment of this unexpected mini-series about mapping. In the previous post, we created a custom codec to store a large number of polylines in size-optimized binary files. Now we will put them on the map. It took me three attempts to come up with something efficient enough to run on mobile devices.
GeoJSON
The initial version used the GeoJSON data source and renderer provided by Mapbox GL JS. It was a breeze to get up and running: decode the binary data and convert it with Turf.js. That was easy, I thought, and enabled all countries one by one until Chrome looked at me with a frowny face!

It seems like the barely used DRAM sitting in my own computer wasn't enough.
But from a global allocation perspective, actually, it’s quite terrible to have everybody have a bunch of the world’s limited supply of DRAM sitting in their own computer. And, being used 5% of the time. You want maximum utilization in the cloud if you’re trying to deal with this very supply constrained environment.
Sam Altman
I wanted to present some accurate numbers here, but the heap size reported by Chrome and its RAM usage in the task manager don't match up at all. So I am skipping this part. Suffice to say, this isn't the solution we are looking for.
deck.gl
The next version used deck.gl, a GPU-powered data visualization framework. Its path layer can render polylines, but showing all of them at full detail simultaneously leads to performance problems on lower-end hardware. So we move over to the tile layer 1 and dynamically create tiles optimized for the current zoom.
The workflow looks something like this:
- Decode the binary data and transfer it to a web worker
- Build a spatial index and calculate Douglas–Peucker weights
- Build zoom-optimized 512x512 pixel tiles on demand
- Render the tiles as individual path layers
Here is what it looks like at low zoom:

The time it takes to generate tiles is heavily dependent on the zoom, because they always have the same pixel dimensions. Therefore, tiles at a low zoom contain more data. A tile covering the whole of Germany is built in around 100 ms, while one at high zoom needs less than a millisecond.

I thought this was the final version until I tested it on my smartphone. The performance was horrible. I guess my old gaming PC 2 is a bad benchmark. So we have to go even deeper down the performance rabbit hole.
custom.gl
As always with code: less is more. External libraries are great for increased development speed and reduced mental load, but sometimes handwriting a custom solution for the hot path is worth it when performance really matters. By nature, it will skip unnecessary abstractions and focus on the main task at hand. In our case, rendering hundreds of thousands of polylines on a map.
Here is the new workflow:
- Decode the binary data
- Create levels of detail for low, medium and high zoom
- Optimize the polylines for the zoom using simplify.js
- Recursively chunk the polylines into quadrants
- Render chunks within the map viewport
This is what it looks like for Germany:

Chunks are recursively divided into quadrants until each one contains less than 250,000 line segments. This prevents lag at high zoom on lower-end hardware. During rendering, the bounds of each chunk are compared to the current viewport, and the ones outside of it are skipped.
for (const chunk of this.lod.chunks) {
if (intersects(this.map.getBounds(), chunk.bounds)) {
gl.bindBuffer(gl.ARRAY_BUFFER, chunk.segmentBuffer);
// Rendering in progress...
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
}
Chunks look nice and uniform for rectangular countries but can become pretty interesting for elongated or island-containing ones. Spain has the Canary Islands in the Atlantic, which extends the bounding box far into the South:

But nothing beats France with its overseas territories and departments:
- Saint Pierre and Miquelon in the North Atlantic Ocean
- French West Indies in the Caribbean
- French Guiana in South America
- French Polynesia, Wallis and Futuna, and New Caledonia in the South Pacific Ocean
- French Southern and Antarctic Lands, Réunion, and Mayotte in the Indian Ocean
Here it is in all its chunky glory:

Conclusion
And that's about it. The data is compressed with a custom codec, and the performance is acceptable on lower-end hardware. An ideal version of poi would use custom vector tiles, but those would take it a step too far and don't work well with the restrictions imposed on the project:
- 100 MB static file limit on Vercel
- Automatic updates with GitHub actions
Endings are hard. Bye.