DEV Community

Ricardo Saumeth
Ricardo Saumeth

Posted on

๐Ÿš€ ๐— ๐—ฒ๐—บ๐—ผ๐—ฟ๐˜†โ€‘๐—•๐—ผ๐˜‚๐—ป๐—ฑ๐—ฒ๐—ฑ ๐—”๐—ฟ๐—ฟ๐—ฎ๐˜†๐˜€

Realโ€‘time applications behave very differently from typical web apps.
A normal React app might fetch data once, store a few dozen items, and never grow beyond that.

A realโ€‘time trading UI is the opposite:

โšก Trades stream in continuously
๐Ÿ“ˆ Order books update dozens of times per second
๐Ÿ•’ Candles accumulate every minute
๐Ÿ–ฅ๏ธ The app stays open for hours, sometimes entire trading sessions

If you store every update forever, your arrays grow like this:

  • 1 minute โ†’ 1,200 updates
  • 1 hour โ†’ 72,000 updates
  • 4 hours โ†’ 288,000 updates
  • 8 hours โ†’ 576,000 updates

And thatโ€™s per currency pair.

Track 10 pairs and youโ€™re easily in the millions of objects.

At that point:

  • The browser slows down
  • Garbage collection spikes
  • Memory usage explodes
  • Rendering becomes inconsistent
  • AG Grid and charts start choking
  • The UI becomes unresponsive

This is why unbounded arrays will kill the browser.

๐Ÿ› ๏ธ What Memoryโ€‘Bounded Arrays Actually Do

A memoryโ€‘bounded array enforces a hard limit on how many items you keep.

Example:

const MAX_TRADES = 1000
if (trades.length > MAX_TRADES) {
// Immediate memory release
trades.splice(0, trades.length - MAX_TRADES)
}

This means:

  • You always keep the latest 1000 trades
  • You never store more than 1000
  • Memory usage stays predictable
  • Rendering cost stays stable
  • The app behaves the same after 5 minutes or 5 hours

This is the difference between a realโ€‘time system and a demo app.

โšก Why This Keeps the UI Responsive

  1. Rendering cost stays constant

React, AG Grid, and Highcharts all scale poorly with huge arrays.

Keeping arrays capped means:

  • No exponential rendering cost
  • No slowdowns over time
  • No UI degradation after long sessions
  1. Memory usage stays flat

Without limits, memory grows forever.
With limits, memory looks like this:

  • Start โ†’ 50MB
  • 1 hour โ†’ 55MB
  • 8 hours โ†’ 60MB

Instead of:

  • Start โ†’ 50MB
  • 1 hour โ†’ 200MB
  • 8 hours โ†’ 1GB+ (browser crash)
  1. Garbage collection stays predictable

Large arrays create GC spikes that freeze the UI.
Bounded arrays avoid this entirely.

  1. Charts and grids stay smooth

Highcharts and AG Grid both struggle with large datasets.

Capping arrays ensures:

Smooth scrolling
Fast sorting
Stable FPS
No jank

  1. The app behaves the same after hours of use

This is the real goal.

A trading UI must be as fast at 4pm as it was at 9am.
Memoryโ€‘bounded arrays guarantee that.

๐Ÿ“ The Principle Behind It

Realโ€‘time systems donโ€™t need infinite history โ€” they need the most recent, relevant data.

Traders care about the last 1000 trades
Order books only need the current snapshot
Charts only need the last N candles

Everything else belongs in a backend database, not the browser.
Memoryโ€‘bounded arrays prevent realโ€‘time UIs from degrading over time.

๐—ช๐—ฟ๐—ถ๐˜๐˜๐—ฒ๐—ป ๐—ฏ๐˜† ๐—ฅ๐—ถ๐—ฐ๐—ฎ๐—ฟ๐—ฑ๐—ผ ๐—ฆ๐—ฎ๐˜‚๐—บ๐—ฒ๐˜๐—ต
๐—ฆ๐—ฒ๐—ป๐—ถ๐—ผ๐—ฟ ๐—™๐—ฟ๐—ผ๐—ป๐˜โ€‘๐—˜๐—ป๐—ฑ ๐—˜๐—ป๐—ด๐—ถ๐—ป๐—ฒ๐—ฒ๐—ฟ | ๐—ฅ๐—ฒ๐—ฎ๐—นโ€‘๐—ง๐—ถ๐—บ๐—ฒ ๐—จ๐—œ ๐—ฆ๐—ฝ๐—ฒ๐—ฐ๐—ถ๐—ฎ๐—น๐—ถ๐˜€๐˜

Top comments (0)