What I learned moving a flight tracker from Leaflet to deck.gl
What happened when I tried to scale my Leaflet flight tracker to 5,000 live aircraft, why putting 60fps movement inside React state breaks down, and how I rebuilt the data pipeline with MapLibre, deck.gl, and typed arrays.
- React
- TypeScript
- MapLibre
- WebGL
- Architecture
Project Links
- Live Demo (Airspace 3D): airspace-v2.vercel.app
- Source Code: https://github.com/netshcodes/airspace-v2
- Original V1 (Leaflet): track-a-flight.vercel.app
I started this project because a recruiter reached out about my first flight tracker (track-a-flight.vercel.app), which I built with Leaflet. In that app, tracking one flight at a time is easy: you fetch coordinates every few seconds and move an SVG marker on raster map tiles.
To push myself and learn WebGL mapping, I decided to build a V2 that tracks 5,000+ live aircraft at once and animates them smoothly at 60 frames per second.
I quickly realized that simply swapping Leaflet for MapLibre wasn't enough. If I structured the data the way I normally structure a React app, the browser would choke before the map even finished loading.
The problem: React state at 60fps
When you build a normal React app, your muscle memory is to put your data in useState or a Zustand store (Aircraft[]), map over it, and let React update the screen.
Here is why that breaks down when rendering 5,000 moving aircraft at 60fps:
- Object churn and garbage collection: Updating 5,000 JavaScript objects 60 times a second means allocating 300,000 new objects every second. The browser's garbage collector has to pause your app to clean them up, causing visible stutter.
- Unnecessary React re-renders: React is built to diff UI trees when a user clicks a button or types in a form—not to reconcile 5,000 map coordinates every 16 milliseconds.
- CPU-to-GPU translation overhead: Even if you use a GPU library like
deck.gl, passing an array of JS objects (getPosition: (d) => [d.lon, d.lat, d.alt]) forces the CPU to loop through those objects and pack them into a binary buffer on every single frame.
I needed a way to move 5,000 planes every frame without React knowing or caring that a frame had passed.
The fix: Split hot data from cold data
As I dug into how WebGL engines and WebAssembly actually consume data, I realized I was lumping two completely different kinds of data together:
- Hot data (changes 60 times a second): Longitude, latitude, altitude, speed, and heading.
- Cold data (changes every 10 seconds): Callsigns, ICAO transponder IDs, and flight metadata.
Instead of an array of objects, I stored the hot data in two flat Float32Array buffers owned by a plain TypeScript class (AirspaceController) completely outside React:
export const POSITION_STRIDE = 3; // [lon, lat, altitudeMeters]
export const KINEMATICS_STRIDE = 2; // [velocityMps, headingDeg]
export interface HotBuffers {
readonly count: number;
readonly positions: Float32Array;
readonly kinematics: Float32Array;
}On every requestAnimationFrame tick, my simulation loop updates the numbers in positions in place. Zero objects are allocated. Callsigns live in a separate metadata array at the exact same index and are only read when a user actually hovers or clicks on a plane.
To get those positions onto the GPU without a CPU loop, I passed the Float32Array directly into deck.gl's binary attribute API:
export function buildAirspaceLayers({
buffers,
generation,
}: BuildAirspaceLayersParams) {
return [
new ScatterplotLayer({
id: AIRCRAFT_LAYER_ID,
data: {
length: buffers.count,
attributes: {
getPosition: {
value: buffers.positions,
size: POSITION_STRIDE,
},
},
},
getFillColor: AIRCRAFT_FILL_COLOR,
getRadius: AIRCRAFT_RADIUS_METERS,
radiusMinPixels: AIRCRAFT_RADIUS_MIN_PX,
radiusMaxPixels: AIRCRAFT_RADIUS_MAX_PX,
pickable: true,
updateTriggers: {
getPosition: generation,
},
}),
];
}Because I mutate positions in place, its JavaScript reference never changes. Incrementing a simple generation counter on each frame tells deck.gl to re-upload the updated buffer to WebGL, drawing all 5,000+ aircraft in a single draw call.
Keeping React out of the animation loop
Once the simulation lived in AirspaceController, React's only job was to mount the map once and step out of the way.
I ran into a subtle React 19 rule here: I initially stored the controller in useRef and read controllerRef.current during render to pass it to my map overlay, which tripped React 19's react-hooks/refs lint rule. Switching to a lazy useState initializer solved it cleanly:
const [controller] = useState(
() => new AirspaceController({ engine: jsSimulationEngine }),
);Inside a bridge component, I subscribed to AirspaceController inside useEffect and called overlay.setProps(...) imperatively on every tick. React never re-renders while the planes fly. For the UI HUD (FPS and compute time), I throttled updates to Zustand to once every 250ms (~4Hz) so the numbers stay readable without thrashing the DOM.
Protecting WebGL from bad API data
While testing with a live snapshot from the OpenSky Network API, I learned that WebGL buffers are unforgiving: if a single NaN or out-of-range coordinate slips into your Float32Array, the entire layer can vanish.
OpenSky returns each flight as a 17-element array full of nullable values. Rather than scattering as number casts across my codebase, I built a strict validation boundary using Zod and TypeScript branded types:
export type Longitude = number & { readonly __brand: "Longitude" };
export type Latitude = number & { readonly __brand: "Latitude" };
export function isLongitude(value: unknown): value is Longitude {
return (
typeof value === "number" &&
Number.isFinite(value) &&
value >= -180 &&
value <= 180
);
}At runtime, a Longitude is just a regular number that packs straight into Float32Array with zero overhead. At compile time, TypeScript guarantees I never accidentally swap lat and lon or pass unvalidated API data into the simulation.
Two real-world bugs I hit along the way
Even after everything compiled cleanly, my first run gave me a pitch-black screen. Debugging it taught me two practical lessons about the current versions of these tools:
deck.glinterleaved: truevs. MapLibre v6: I originally enabledinterleaved: trueonMapboxOverlaysodeck.gland MapLibre would share a single WebGL context. MapLibre v6 changed its internal camera properties (_nearZandheight), causingdeck.glto crash every frame. Settinginterleaved: falsegavedeck.glits own synced WebGL2 overlay canvas and immediately brought the aircraft on screen.- MapLibre's Web Worker in Vite 8: MapLibre parses vector tiles in a background Web Worker so the main thread stays fast. Vite 8's dev bundler broke MapLibre's default inline worker URL (
Worker failed to load). Importing MapLibre v6's worker explicitly with Vite's?worker&urlsuffix and registering it before React mounts fixed both dev and production builds:
import { setWorkerUrl } from "maplibre-gl";
import maplibreWorkerUrl from "maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url";
setWorkerUrl(maplibreWorkerUrl);What I'm building next
Structuring the simulation around a HotBuffers Float32Array layout wasn't just for JavaScript performance—it was preparation for WebAssembly. Next, I'm going to build a 25,000-aircraft stress test and write a Rust/WASM dead-reckoning engine that operates on the exact same memory buffer, so I can toggle between JS and WASM live on the map and measure the difference.
Why I built this: I wanted to take my original Leaflet flight tracker (track-a-flight.vercel.app) and learn how real-time geospatial apps handle thousands of moving entities. Moving the 60Hz loop out of React state and into flat typed arrays was the key shift that made the rest of the stack click.