DEV Community

Cover image for ๐—›๐—ผ๐˜„ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐—ฝ๐—ฟ๐—ฒ๐˜ƒ๐—ฒ๐—ป๐˜ ๐—จ๐—œ ๐—น๐—ฎ๐—ด ๐˜„๐—ต๐—ฒ๐—ป ๐—ฟ๐—ฒ๐—ฐ๐—ฒ๐—ถ๐˜ƒ๐—ถ๐—ป๐—ด ๐—ผ๐˜ƒ๐—ฒ๐—ฟ ๐Ÿญ๐Ÿฌ๐Ÿฌ ๐—ช๐—ฒ๐—ฏ๐—ฆ๐—ผ๐—ฐ๐—ธ๐—ฒ๐˜ ๐—ฒ๐˜ƒ๐—ฒ๐—ป๐˜๐˜€ ๐—ฝ๐—ฒ๐—ฟ ๐˜€๐—ฒ๐—ฐ๐—ผ๐—ป๐—ฑ?
Kiran
Kiran

Posted on

๐—›๐—ผ๐˜„ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐—ฝ๐—ฟ๐—ฒ๐˜ƒ๐—ฒ๐—ป๐˜ ๐—จ๐—œ ๐—น๐—ฎ๐—ด ๐˜„๐—ต๐—ฒ๐—ป ๐—ฟ๐—ฒ๐—ฐ๐—ฒ๐—ถ๐˜ƒ๐—ถ๐—ป๐—ด ๐—ผ๐˜ƒ๐—ฒ๐—ฟ ๐Ÿญ๐Ÿฌ๐Ÿฌ ๐—ช๐—ฒ๐—ฏ๐—ฆ๐—ผ๐—ฐ๐—ธ๐—ฒ๐˜ ๐—ฒ๐˜ƒ๐—ฒ๐—ป๐˜๐˜€ ๐—ฝ๐—ฒ๐—ฟ ๐˜€๐—ฒ๐—ฐ๐—ผ๐—ป๐—ฑ?

"๐—›๐—ผ๐˜„ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐—ฝ๐—ฟ๐—ฒ๐˜ƒ๐—ฒ๐—ป๐˜ ๐—จ๐—œ ๐—น๐—ฎ๐—ด ๐˜„๐—ต๐—ฒ๐—ป ๐—ฟ๐—ฒ๐—ฐ๐—ฒ๐—ถ๐˜ƒ๐—ถ๐—ป๐—ด ๐—ผ๐˜ƒ๐—ฒ๐—ฟ ๐Ÿญ๐Ÿฌ๐Ÿฌ ๐—ช๐—ฒ๐—ฏ๐—ฆ๐—ผ๐—ฐ๐—ธ๐—ฒ๐˜ ๐—ฒ๐˜ƒ๐—ฒ๐—ป๐˜๐˜€ ๐—ฝ๐—ฒ๐—ฟ ๐˜€๐—ฒ๐—ฐ๐—ผ๐—ป๐—ฑ?"

They clarified that the issue is not related to the server or backend but rather how to approach and resolve it on the frontend.

This made me think for a while, and then I realized that we often use these techniquesโ€”knowingly or unknowingly. We are familiar with these concepts, but they often come with technical jargon.

Let me explain these techniques as simply as possible.

To stop UI lag from 100+ WebSocket events per second, you must decouple the high-frequency message stream from the main rendering thread.
Throttle or buffer the events, batch your DOM updates using requestAnimationFrame, and offload heavy processing to a Web Worker.

Implementing these targeted strategies will stabilize your frame rate:

1. Throttle or Buffer the Event Stream
Never update the UI on every single WebSocket message. At 100+ events per second, your browser is forced into constant layout recalculations (reflows) and repaints.
ย ย โ€ข ๐—ง๐—ต๐—ฟ๐—ผ๐˜๐˜๐—น๐—ถ๐—ป๐—ด: Use libraries like Lodash to limit state updates to a maximum of 10โ€“15 frames per second
ย ย โ€ข ๐—•๐˜‚๐—ณ๐—ณ๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด: Push incoming events into an array. Set up an interval or a requestAnimationFrame loop that drains this array, processes the aggregated data, and updates the UI once per cycle.

๐Ÿฎ. ๐—•๐—ฎ๐˜๐—ฐ๐—ต ๐——๐—ข๐—  ๐—จ๐—ฝ๐—ฑ๐—ฎ๐˜๐—ฒ๐˜€
If you are updating UI components individually for every event, you will overwhelm the event loop.
ย ย โ€ข Batch multiple data points into a single state update.
ย ย โ€ข Use requestAnimationFrame to schedule your DOM updates so they align perfectly with the browser's refresh rate.

๐Ÿฏ. ๐—ข๐—ณ๐—ณ๐—น๐—ผ๐—ฎ๐—ฑ ๐—›๐—ฒ๐—ฎ๐˜ƒ๐˜† ๐—ฃ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€๐—ถ๐—ป๐—ด ๐˜๐—ผ ๐—ฎ ๐—ช๐—ฒ๐—ฏ ๐—ช๐—ผ๐—ฟ๐—ธ๐—ฒ๐—ฟ
ย ย โ€ข If you need to parse, filter, or transform the incoming payload data before displaying it, do not do this on the main JavaScript thread.
ย ย โ€ข Move your WebSocket connection and data parsing logic into a Web Worker.
ย ย โ€ข The worker can handle the 100+ events per second, aggregate or reduce the data, and only send a condensed, render-ready payload back to the main UI thread periodically.

๐Ÿฐ. ๐—ข๐—ฝ๐˜๐—ถ๐—บ๐—ถ๐˜‡๐—ฒ ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜๐—ถ๐˜ƒ๐—ถ๐˜๐˜† ๐—ฎ๐—ป๐—ฑ ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐—ป๐—ฒ๐—ป๐˜ ๐—ฅ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด
If you are using frameworks like React, Vue, or Angular, uncontrolled state updates will trigger massive re-render cascades.
ย ย โ€ข ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜: Use useMemo and useCallback to prevent unnecessary component re-renders. Avoid storing rapidly changing WebSocket data directly in component state; instead, use a mutable ref for the raw data and sync to state on a throttled interval.
ย ย โ€ข ๐—ฉ๐—ถ๐—ฟ๐˜๐˜‚๐—ฎ๐—น๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ป: If rendering these events in a list or table, use windowing libraries (like TanStack Table or Virtuoso) so the DOM only renders the rows currently visible on the screen.

๐Ÿฑ. ๐—จ๐˜๐—ถ๐—น๐—ถ๐˜‡๐—ฒ ๐——๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜ ๐—™๐—ฟ๐—ฎ๐—ด๐—บ๐—ฒ๐—ป๐˜๐˜€
For vanilla JavaScript implementations, avoid appending elements to the DOM in a loop. Instead, create a DocumentFragment, append your newly processed elements to it, and then append the entire fragment to the live DOM in one single operation

WebSockets #ReactJS #Frontend #JavaScript #Performance #SystemDesign #InterviewPrep #EngineeringMindset

Top comments (0)