· via dev.to (home feed)
Node 26.12.0 quietly adds native WebSocket frame masking to node:http
Node.js 26.12.0 quietly added websocketMask() and websocketUnmask() to node:http, giving the ws library a native masking path that benchmarks tens of times faster than its JavaScript fallback on larger frames.

Node.js 26.12.0 added two low-key functions to the node:http module: websocketMask() and websocketUnmask(). According to a dev.to writeup that pulled the change apart, the pair landed as an unexplained SEMVER-MINOR entry in the changelog, well below the headline items that usually get summarised — this despite Node 26.x having entered long-term support at the end of October.
What the functions actually do
The scope is deliberately narrow. Each function XORs a buffer against a repeating 4-byte key, either in place or into a separate output buffer. That is exactly the masking operation the WebSocket protocol requires: under RFC 6455, section 5.3, every client must mask a frame's payload before sending, and every server must undo that masking on the way in.
Until this release, the fast path for that operation in Node meant installing bufferutil, an optional native addon that the popular ws package loads when present. Without it, ws silently falls back to a plain per-byte JavaScript loop and, as the dev.to author notes, never tells you it happened. The author also points out that ws 8.18.0 did not always use bufferutil even when it was installed, only invoking it above self-chosen thresholds of 48 bytes for masking and 32 bytes for unmasking, on the theory that crossing into native code costs more than the loop saves on small payloads.
The measured gap
The dev.to post benchmarks both new functions against ws's JavaScript fallback, running on Node 26.13.1 inside a node:26-slim container across payload sizes from 16 bytes to 1 MB, with 5,000 warm-up calls per side. For masking:
- 16 bytes: 41 ns for the builtin against 22 ns for the loop — the loop wins
- 32 bytes: 35 ns against 49 ns
- 256 bytes: 37 ns against 293 ns, roughly 8x
- 4 KB: 117 ns against 4,516 ns, 38x
- 64 KB: 1.7 µs against 76.4 µs, 45x
- 1 MB: 46.5 µs against 1.21 ms, 26x
Unmasking follows the same shape with its crossover one size band higher: the loop still wins at 16 bytes, the two are roughly even at 32, and the builtin pulls ahead by 35x at 4 KB and 43x at 1 MB. The author is careful to flag these as measurements of ws specifically, not a survey of every WebSocket library, so the cost of the fallback path elsewhere in the ecosystem is unmeasured.
ws has already switched
ws 8.22.1, released on 31 October — about two and a half weeks after Node 26.12.0 shipped the builtin, per the post — added feature detection to its buffer utilities. If websocketMask is present on the http module, ws uses it for payloads of 24 bytes and above; below that it runs its JavaScript loop, and on older Node it falls through to bufferutil and then the plain loop. The maintainers' 24-byte threshold sits close to the roughly 32-byte crossover the author measured independently, and ws now prefers the builtin over bufferutil whenever it is available. The current npm release, 8.23.1, keeps the same logic.
Strict errors and overlapping buffers
The dev.to author also probed the failure modes. A 3-byte mask key raises a TypeError citing the received buffer; a length exceeding the source or output buffer, or a negative length, raises a RangeError that quotes the offending value. All are conditions a frame parser could plausibly hit on a malformed or truncated header, and a typed error with the exact bad value beats a silent out-of-bounds write.
The documentation additionally allows source and output buffers to overlap, promising the result matches what you would get by copying the source first. The author tested this with two views into one buffer, offset by a byte and overlapping by nine, and found a byte-for-byte match against masking into an independent copy — an aliasing edge case that a hand-rolled implementation can easily get wrong without anyone noticing.
Why it matters
For a chat app sending mostly small JSON frames, the practical consequence is deployment shape: a WebSocket server no longer needs a compiled addon — or the build toolchain that comes with it — in its Docker image to get fast masking. For anything moving binary payloads in the kilobyte-to-megabyte range, the built-in functions are tens of times faster than the pure-JavaScript path. Because ws already prefers the builtin when present, most users get the benefit on upgrade without changing code, provided they run Node 26.12.0 or later. The small-frame regime is the honest exception: at 16 bytes the plain loop is measurably faster, which is exactly why ws keeps a size threshold rather than always dropping into native code.
- #node-js
- #websocket
- #javascript
- #performance