Client‑side versus Server‑side Time‑Zone Handling for Mapbox GL JS Temporal Layers
0 reputation · 26 Nov 2022, 14:40 UTC
0 reputation · 26 Nov 2022, 14:40 UTC
Goal: Allow map viewers to switch between time zones for temporal data displayed in Mapbox GL JS without sacrificing frame‑rate or introducing date‑offset errors.
Constraint: Performing the conversion client‑side with new Date() gives each user zone flexibility but adds per‑feature CPU work and can produce different historic offsets across browsers because each implements its own IANA database version. Pre‑converting timestamps server‑side (e.g., storing UTC strings in vector tile properties) removes the per‑frame cost but locks the map to a single zone, requiring a full tile rebuild whenever the target zone changes.
Which strategy provides the best trade‑off between dynamic user zone selection and render performance for large feature sets? How can historic offset inconsistencies be minimized when relying on client‑side Date objects?
34775 reputation · 26 Nov 2022, 20:13 UTC
The best trade-off between dynamic user selection and render performance is to store timestamps as numeric UTC integers (Unix epoch) within vector tile properties and apply a single, global offset variable on the client side. This avoids the need to rebuild tiles on the server while bypassing the CPU overhead of per-feature Date object instantiation during the render loop.
Instead of converting every feature's timestamp to a local time zone, treat the time zone selection as a mathematical shift applied to the filter criteria rather than the data itself.
setFilter(). For example, if the user wants to see data for "12:00 PM EST," calculate the UTC equivalent and filter the numeric properties against that single UTC value.Relying on the native Date object is risky for historical data because different browser engines may use different versions of the IANA Time Zone Database. To ensure deterministic behavior across all clients:
Intl implementation.new Date() inside the map.on('render', ...) loop. Perform all time-zone calculations once per UI change and store the resulting numeric UTC timestamp in a variable.To verify this approach, you can monitor the frame rate (FPS) using the browser's performance profiling tools. Compare the cost of setFilter(['>=', 'timestamp', utcValue]) against a custom filter that invokes new Date() for every feature. The numeric comparison is handled by the Mapbox GL JS engine (often via GPU/WebGL), whereas Date objects force a move back to the main CPU thread.
Diagnostic Detail Needed: Are you utilizing Mapbox's built-in setFilter for visibility, or are you manually iterating through features to update styles/properties via a custom render loop?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.