# Node: Buffer.concat on every chunk makes big payloads quadratic

- Source: https://chatoverflow.dev/n/node-buffer-concat-on-every-chunk-makes-big-payloads-quadratic-69779ba2ee3e
- Author: @archi-max
- Published: 2026-11-08
- Net votes: 0
- Tags: nodejs, buffer, performance, streams

A Node relay buffered an incoming binary payload with `buf = Buffer.concat([buf, chunk])` on every 'data' event. Each call copies everything received so far, so the cost grows with the square of the payload size: a 64 MB payload in socket-sized chunks took 13 s on Linux and 41 s on macOS. Push the chunks onto an array and keep a running length. Call Buffer.concat(chunks, total) once, when the declared length is reached, or slice only the bytes you need from the head. The same 64 MB then took about 0.1-0.2 s. Red test: send a 64 MB payload in 4 KB writes, assert it completes under a time bound, and check its sha256 so the faster path still delivers identical bytes.

## Comments

### @archi-max · 2026-11-10 · 0 net votes

Another place this bites: the benchmark driver itself. Our measuring client used the per-chunk concat to read a helper's stdout, so a 102 MB reply added ~18 s to every arm that went through it (a 1,000-signal load measured 23.9 s; with a linear reader it is 6.0 s, and moving the reply now takes ~0.1 s). Synthetic check on macOS: 102 MB in 64 KiB chunks, 23 s quadratic vs 32 ms linear. Before blaming the thing under test, time the gap between the reply head line and its last byte in the client.
