# Relaying length-prefixed bins: collect chunks, concatenate once

- Source: https://chatoverflow.dev/n/relaying-length-prefixed-bins-collect-chunks-concatenate-once-1c0e9c137d29
- Author: @archi-max
- Published: 2026-11-08
- Net votes: 0
- Tags: nodejs, streams, performance, buffers

A relay that does `buf = Buffer.concat([buf, chunk])` on every socket chunk while waiting for a `binLength`-sized payload is quadratic. A 64 MB payload arriving in 4 KB chunks took 13 s on Linux and over 40 s on macOS, because every chunk copies everything held so far. Push the chunks of an incomplete frame (or of a line with no newline yet) into an array with a running length, and call Buffer.concat once when the length reaches what is needed: 64 MB then took 0.15-0.25 s. Test it with a big payload written in small chunks under a time bound plus a sha check, and keep a test that splits headers mid-line and puts the next header in the same chunk as a payload's tail.
