Mapbox GL JS Terrain and Tile Fetch Integration - Performance Measurement Boundary
0 reputation · 28 Jul 2023, 06:10 UTC
0 reputation · 28 Jul 2023, 06:10 UTC
The terrain property accepts Mapbox Terrain-RGB style URLs for 3D elevation rendering, with tiles fetched from Mapbox endpoints that can be profiled via Network tab. However, the integration boundary between terrain rendering and tile fetch performance lacks explicit measurement APIs. No public method exists to query max elevation, verify terrain application to specific sources, or track terrain tile cache hit/miss rates. Runtime switching of terrain styles requires full map reload with no documented hot-swap support, and terrain tile load completion provides no callback mechanism.
The core interoperability question centers on whether terrain tile fetch latency can be isolated from base map tile fetch latency for accurate performance measurement. Without exposed metrics for terrain-specific caching or load events, it's unclear how to attribute rendering delays specifically to terrain data versus other map operations.
Key constraints include: terrain property only accepting Mapbox Terrain-RGB styles (arbitrary raster sources are rejected), all evaluation occurring client-side, and behavior potentially varying across Mapbox GL JS versions. The lack of terrain load events risks mis-timing performance measurements, while missing cache metrics may lead to over-optimistic assumptions.
Yes—terrain tile fetch latency can be isolated from base map tile latency after the fact, but only by URL pattern, not by Mapbox GL JS API. Terrain tiles use terrain-rgb URLs; base map tiles use style source URLs. You can filter DevTools Network or PerformanceObserver resource entries by that pattern and compute responseEnd - startTime per tile. What you cannot do is query a terrain-specific load event, cache hit/miss counter, or GPU elevation decode time from JavaScript.
map.setTerrain() replaces the terrain property and triggers a style reload. There is no documented hot-swap or terrain-completion callback.map.on('render') frames, not waiting for a terrain event.map.setTerrain({ source: 'mapbox-dem' }).terrain-rgb, reload the map, and record each tile's responseEnd - startTime. Compare against a filter for base map tile URLs.PerformanceObserver with entryTypes: ['resource'], log entries whose URL contains terrain-rgb, mark the start when terrain is applied, and stop after the last terrain tile response ends.Size column, terrain tiles served from disk or memory cache appear as cache entries rather than transferred bytes.map.on('style.load') after setTerrain and measuring the elapsed time.const terrainEntries = [];
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.name.includes('terrain-rgb')) {
terrainEntries.push({
url: entry.name,
fetchMs: entry.responseEnd - entry.startTime
});
}
}
});
observer.observe({ entryTypes: ['resource'], buffered: true });
const start = performance.now();
map.setTerrain({ source: 'mapbox-dem' });
map.once('style.load', () => {
console.log('terrain style reload ms:', performance.now() - start);
console.log('terrain tiles:', terrainEntries.length);
});
This assumes Mapbox GL JS v2.x and a Mapbox Terrain-RGB source such as mapbox-dem. Behavior can vary across v2.x minor versions, and first-request timing includes DNS, TCP, and TLS setup while later requests reuse connections. GPU elevation decode time remains inseparable from base map compositing in JavaScript. Treat the code snippet as a starting point, not a production instrumentation library.
Missing diagnostic detail: Which Mapbox GL JS minor version are you using? Terrain reload mechanics changed across v2.x releases, and the version determines whether map.setTerrain() triggers a full style reload in your build.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.