Preventing React Virtual DOM starvation during high-frequency industrial sensor streams using Float32Array circular ring buffers and requestAnimationFrame-throttled canvas rendering.
When High-Frequency Telemetry Stalls the Browser
Industrial equipment — such as turbines, CNC spindles, and compressors — transmits sensor telemetry at 100 to 500 samples per second per channel. Standard web dashboards typically bind incoming data to React state (useState) and render SVG charts.
During a contract project developing industrial telemetry dashboards (Feb–Mar 2025), I was tasked with investigating why a high-frequency sensor dashboard was stuttering under live feeds. Profiling in Chrome DevTools showed the interface dropping to ~14 FPS with ~82% main-thread CPU utilization, accompanied by frequent garbage collection pauses.
Recreating and diffing thousands of DOM/SVG nodes at 200 Hz quickly overwhelms browser layout and compositing pipelines. Before moving to HTML5 Canvas, we first attempted optimizing React state using useRef for data accumulation paired with throttled updates (30 Hz) and memoized SVG components. While this reduced render frequency, managing 10+ concurrent channels still caused visible frame drops during burst traffic. Moving visualization entirely to an HTML5 Canvas backed by a fixed ring buffer delivered predictable 60 FPS performance without React reconciler overhead.
Stream Isolation Objective
Decoupling network ingestion from React state allows the dashboard to absorb 200–500 sensor samples per second while canvas drawing runs smoothly aligned to monitor refresh rates.
Decoupling Ingestion: Float32Array Ring Buffers
Pushing incoming telemetry into dynamic JavaScript arrays causes continuous heap reallocations as arrays grow, triggering frequent V8 garbage collection cycles. On performance timelines, these appear as noticeable micro-stutters.
Instead, we pre-allocate a fixed-capacity Float32Array holding 1,000 samples. New points overwrite the oldest values using modulo pointer arithmetic: head = (head + 1) % capacity. Appending a point is an O(1) operation with zero memory allocation.
WebSocket messages update this buffer directly in memory without calling setState. A separate requestAnimationFrame loop reads the current window from the buffer and paints directly onto an HTML5 Canvas. This aligns drawing frequency with the display's actual refresh rate (whether 60 Hz, 120 Hz, or 144 Hz) rather than the packet arrival rate.
utils/telemetry_ring_buffer.ts (Circular Ring Buffer)typescriptExpand excerptCollapse excerpt
export class CircularTelemetryBuffer {
private buffer: Float32Array;
private head: number = 0;
private count: number = 0;
private capacity: number;
constructor(capacity: number = 1000) {
this.capacity = capacity;
this.buffer = new Float32Array(capacity);
}
// O(1) append with zero heap reallocations
public push(val: number): void {
this.buffer[this.head] = val;
this.head = (this.head + 1) % this.capacity;
if (this.count < this.capacity) this.count++;
}
public render(ctx: CanvasRenderingContext2D, width: number, height: number): void {
const step = width / (this.capacity - 1);
const startIdx = this.count === this.capacity ? this.head : 0;
ctx.beginPath();
for (let i = 0; i < this.count; i++) {
const val = this.buffer[(startIdx + i) % this.capacity];
const y = height - (val / 100.0) * height;
i === 0 ? ctx.moveTo(0, y) : ctx.lineTo(i * step, y);
}
ctx.stroke();
}
}Chrome DevTools Profiler Measurements
Measurements recorded over 60-second profiles on a Next.js production build using Chrome DevTools Performance & Memory profilers (macOS Apple Silicon, continuous 200 Hz synthetic sensor stream, median of 3 runs):
| Performance Metric | Standard React State (useState) | Decoupled Ring Buffer + Canvas | Result |
|---|---|---|---|
| Display Frame Rate | ~14 FPS (Severe stuttering) | ~60 FPS (Stable rendering) | ~4.3x smoother |
| Main Thread CPU Utilization | ~82% (Thread contention) | ~9% (Lightweight) | ~89% CPU reduction |
| JS Heap Footprint | ~74 MB (Continuous array churn) | ~28 MB (Flat baseline, 0 churn) | Zero telemetry allocations |
| GC Pause Duration | ~45–90 ms (Frequent minor GC) | < 3 ms (Negligible) | Eliminated GC stutter |
| Packet Ingestion Latency | ~120 ms (Queue backlog) | < 5 ms (Direct memory write) | Real-time signal fidelity |
Circular Ring Buffer vs. Naive React State (100 Hz Ingestion)
The Background Tab Trap & Page Visibility API
After deploying the canvas plotter, we noticed client laptop fans ramped up when operators left dashboards open in background browser tabs. While browsers throttle requestAnimationFrame in inactive tabs, background telemetry streams were still triggering canvas calculations.
We resolved this using the Page Visibility API. When document.hidden is true, the canvas draw loop pauses completely while the ring buffer continues absorbing incoming WebSocket data in memory. Once the user returns to the tab, visual rendering resumes instantly at current values without telemetry loss.
Trade-off: Direct canvas rendering bypasses React's declarative component model. Interactive features such as hover tooltips require manual coordinate math (hit-testing) rather than built-in DOM event listeners. For high-frequency telemetry, however, the rendering stability significantly outweighs the added development overhead.
