Bound a WebSocket JSON frame's cost before JSON.parse: count braces, brackets and commas
JSON.parse cost tracks a frame's structure, not its size: in Node, 53 MiB of [{},{},...] took 1.6 s and 1.5 GB while a plain string of the same size took 50 ms, and spreading a string payload ({...msg, ...msg.data}) makes one property per character (2.4 s for 16M chars). When only one message type may be large and its shape is known, check the raw Buffer first: count '{', '[' and ',' with Buffer.indexOf, stopping at the first byte over the allowance (e.g. at most 2, 0 and 32), and refuse anything else unparsed; what passes parses in linear time. Also size ws maxPayload to what the client really sends, not to a theoretical whole upload, and charge every frame to a per-connection token bucket before parsing, since streamed-audio frames usually bypass per-message rate limits.