Streaming an upload into sharp saved me about 9% of peak memory, not the order of magnitude people expect. Eight parallel uploads of a 65.5 MB PNG peaked at 1,533 MB when I buffered them and 1,398 MB when I piped them. The same eight uploads were refused at 85 MB by checking Content-Length before reading a byte. The cheap check beat the clever one.
Below is the server I tested, the five cases, and what to put at the front of your upload route.
Why would streaming not save memory?
Decoding dominates. A 65.5 MB PNG is 8000 × 6250 pixels, and in my companion measurement of a 50 MP decode a full decode of that size needs about 150 MB of raw pixels. Eight of those in flight is over a gigabyte, whether the compressed bytes arrived in one Buffer or in chunks. The compressed body is 8 × 65.5 MB = 524 MB on top.
The point here is that you cannot stream your way out of a decode.
What did I test?
A plain node:http server with two routes, no framework, so only sharp and the body handling are in play:
// server.mjs (node server.mjs <port> <maxBytes> [limitInputPixels]) import http from 'node:http'; import sharp from 'sharp'; const MAX = Number(process.argv[3]); const LIMIT = process.argv[4] ? Number(process.argv[4]) : undefined; const opts = LIMIT ? { limitInputPixels: LIMIT } : {}; sharp.cache(false); const server = http.createServer(async (req, res) => { const done = (code, body) => { res.writeHead(code, { 'content-type': 'application/json', connection: 'close' }); res.end(JSON.stringify(body)); }; // Refuse early when the client declares too much. if (Number(req.headers['content-length']) > MAX) return done(413, { error: 'declared too large' }); try { if (req.url === '/buffer') { const chunks = []; let n = 0; for await (const c of req) { n += c.length; if (n > MAX) { req.destroy(); return; } chunks.push(c); } const out = await sharp(Buffer.concat(chunks), opts).resize(1920).webp({ quality: 80 }).toBuffer(); return done(200, { in: n, out: out.length }); } if (req.url === '/stream') { let n = 0; const t = sharp(opts).resize(1920).webp({ quality: 80 }); req.on('data', (c) => { n += c.length; if (n > MAX) { req.unpipe(t); t.destroy(); req.destroy(); } }); req.pipe(t); const out = await t.toBuffer(); return done(200, { in: n, out: out.length }); } } catch (e) { if (!res.headersSent) done(422, { error: String(e.message).split('\n')[0] }); } }); server.listen(Number(process.argv[2])); The server samples its own resident memory every 5 ms and reports the peak. A client sends the same file N times in parallel with http.request. I started a fresh server for every run and took the median of 5.
What were the results?
Eight parallel uploads of p50.png (65,506,064 bytes, 50 megapixels):
| Case | Status | Server peak | Slowest client |
|---|---|---|---|
| buffer, no pixel cap | 200 | 1,533 MB | 1,803.2 ms |
| stream, no pixel cap | 200 | 1,398 MB | 1,644.3 ms |
buffer, limitInputPixels 20,000,000 | 422 | 1,147 MB | 282.3 ms |
stream, limitInputPixels 20,000,000 | 422 | 1,144 MB | 275.7 ms |
client sends Content-Length, limit 20 MB | 413 | 85 MB | 14.7 ms |
- Streaming saved 135 MB (1,533 against 1,398) and 160 ms. Useful, not decisive.
- A pixel cap refused the image fast but not cheaply. Both routes returned 422 in under 300 ms, yet peaked at about 1,145 MB. The 524 MB of bodies had already arrived and sat in memory. That the stream route did the same suggests sharp takes the whole stream before it rejects; I did not trace the cause.
- The
Content-Lengthcheck is the winner. It returned 413 in 14.7 ms and the server stayed at 85 MB.
Note: a client can leave out
Content-Lengthand send the body chunked. I tested that: 4 clients each sent a 300,000,000-byte file of random bytes with a 10,000,000-byte cap on the server. Both routes cut the connection after about 10 MB, in 34.7 ms (buffer) and 33.7 ms (stream), peaking at 124 MB. The clients sawEPIPEorECONNRESET, not a status code. So the byte counter, not the header, is your real limit.
What should you do?
- Check
Content-Lengthfirst and return 413. It is the cheapest line in the handler and it worked on all 8 uploads. - Count bytes as they arrive and cut the connection at your limit. The header can lie or be absent.
- Pick the streaming route if you like it, but do not expect a large memory win from sharp.
- Cap pixels as well, with
limitInputPixels. It ended 8 oversized decodes in under 300 ms instead of 1.6 s or more. Reading the size from the header first is the next step: see the companion piece on rejecting oversized images withmetadata(). - Limit parallel requests. Peak memory scaled with the number in flight.
Our own API applies size and pixel limits before processing; the how we measure page explains how we count.
How I measured
- Machine: Apple M1 Pro, 8 cores, 16 GB, macOS, 2026-10-18. Client and server on the same machine, over
localhost. - Versions: Node 24.22.0, sharp 0.35.4, libvips 8.18.6.
- Input:
p50.png, 8000 × 6250, built from our camera photo with noise added (noise overlaid on our camera photo, as in the companion decode article);junk.bin, 300,000,000 random bytes from/dev/urandom. - Peak: the server's resident set size sampled every 5 ms with
process.memoryUsage(). Median of 5 fresh servers per case; times are the slowest client in each run, then the median. Raw output inmeasured.json.
What I did not test
Fastify, Express or multipart parsers such as busboy. A real multipart upload adds its own buffering. I also did not test slow clients, a reverse proxy in front, or different sharp stream settings.
I build Blinkimg, an image compressor and converter with a developer API. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.

