Menu

1 / 2

Stream or buffer image uploads to sharp? Peak memory, measured

DEV Community: node·Sahadat Husain·about 8 hours ago
#wOB37eSM
#devtonodejs#devto#dev#server#sharp
Long article (1208 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.