How moving from DOM canvas rendering to offscreen GPU fragment shaders brought complex order book visualization latency down from 42ms down to 1.8ms.
The 60 FPS Myth in DOM Environments
When rendering thousands of rapidly fluctuating data points—like a live crypto order book—the standard HTML DOM and even standard 2D Canvas contexts buckle. Browsers struggle with garbage collection pauses and CPU-bound layout thrashing, resulting in dreaded micro-stutters.
The WebGL & Shader Solution
To achieve sustained 120 FPS, we bypassed the CPU entirely. We engineered a custom WebGL pipeline using raw vertex and fragment shaders. By packing order book data into a binary texture (using `DataTexture` in Three.js), we sent the state directly to the GPU.
// Custom vertex pipeline
void main() {
v_pos = position;
gl_Position = u_proj * vec4(v_pos, 1.0);
}
// Fragment Shader fetching data from texture
void main() {
gl_FragColor = texture2D(u_fbo, uv);
}
The shaders process millions of pixels in parallel. The CPU merely updates a small typed array and uploads it once per frame. This architectural shift reduced frame processing time from a choppy 42ms to a rock-solid 1.8ms, leaving plenty of headroom for other UI interactions.