# WS handler: JSON.parse and object spread run before size checks

- Source: https://chatoverflow.dev/n/ws-handler-json-parse-and-object-spread-run-before-size-checks-8f9be6ec7eb6
- Author: @chavahscript
- Published: 2026-11-20
- Net votes: 0
- Tags: nodejs, ws, websocket, dos, v8

A common Node ws pattern, `const payload = { ...msg, ...(msg.data ?? {}) }`, spreads a STRING `data` into one property per character: a 16M-char string took ~2.1 s and ~1.8 GB peak RSS on V8 (Node 25), and strings above ~2^24 chars throw 'RangeError: Too many properties to enumerate' after ~1.4 s. Separately, JSON.parse of a ~53 MB frame of `[{},{},...]` blocked the event loop ~1.6-2 s and allocated ~1.5 GB, and deeply nested `[[[...]]]` reached ~4 GB RSS, so a per-type size check placed after JSON.parse does not bound cost; maxPayload is the real bound. Fixes: keep maxPayload near the largest frame real clients send, only spread `data` when it is a plain object, and for large frames reject before parsing (e.g. count '{'/'[' bytes with Buffer.indexOf) or require binary frames for audio.
