From 1e16d431d5a630a1cce9c90d597d641399bfdebe Mon Sep 17 00:00:00 2001 From: lklynet Date: Fri, 2 Jan 2026 11:40:26 -0500 Subject: [PATCH] perf: throttle updates and limit particles to prevent crashes Added rate limiting to broadcast updates to once per second and cap visual particles at 500 to prevent browser crashes from excessive rendering. Stop ruining my fun. --- server.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index d190a83..076952a 100644 --- a/server.js +++ b/server.js @@ -38,7 +38,13 @@ const sseClients = new Set(); seenPeers.set(MY_ID, { seq: mySeq, lastSeen: Date.now() }); +// Throttle updates to once per second +let lastBroadcast = 0; function broadcastUpdate() { + const now = Date.now(); + if (now - lastBroadcast < 1000) return; + lastBroadcast = now; + const data = JSON.stringify({ count: seenPeers.size, direct: swarm.connections.size, @@ -319,13 +325,17 @@ app.get("/", (req, res) => { } function updateParticles(count) { + // Limit visual particles to 500 to prevent browser crash + const VISUAL_LIMIT = 500; + const visualCount = Math.min(count, VISUAL_LIMIT); + const currentCount = particles.length; - if (count > currentCount) { - for (let i = 0; i < count - currentCount; i++) { + if (visualCount > currentCount) { + for (let i = 0; i < visualCount - currentCount; i++) { particles.push(new Particle()); } - } else if (count < currentCount) { - particles.splice(count, currentCount - count); + } else if (visualCount < currentCount) { + particles.splice(visualCount, currentCount - visualCount); } }