Tips > Reliability & Performance

Compress Binary Data Before Storing or Transferring

Transferring large binary files between nodes, sending them as email attachments, or uploading to cloud storage is slow and bandwidth-intensive.

Transferring large binary files between nodes, emailing them, or uploading them to cloud storage is slow and bandwidth-heavy. Add a compression step whenever the downstream consumer supports it: a Code node can gzip binary data before transfer, and the Edit Image node can resize or re-quality images. This can cut a multi-file email from 80 MB to 8 MB.

Why compress binary data before transferring it?

Transferring large binary files between nodes, sending them as email attachments, or uploading to cloud storage is slow and bandwidth-intensive. Add a compression step when the downstream consumer supports it.

How do you gzip binary data in a Code node?

Real-world example: A reporting workflow generates 15 CSV exports (2-10 MB each) and emails them to stakeholders. Without compression, the email is 80 MB and times out. With gzip, the total drops to 8 MB.

// Code node: Compress binary data
// Mode: Run Once for All Items
const zlib = require('zlib');

const results = [];

for (const item of $input.all()) {
  if (item.binary?.data) {
    const binaryData = await this.helpers.getBinaryDataBuffer(
      $input.all().indexOf(item),
      'data'
    );

    const compressed = zlib.gzipSync(binaryData);

    results.push({
      json: {
        ...item.json,
        originalSize: binaryData.length,
        compressedSize: compressed.length,
        compressionRatio: ((1 - compressed.length / binaryData.length) * 100).toFixed(1) + '%',
      },
      binary: {
        data: await this.helpers.prepareBinaryData(
          compressed,
          item.binary.data.fileName + '.gz',
          'application/gzip'
        ),
      }
    });
  } else {
    results.push(item);
  }
}

return results;

How do you compress images specifically?

For image-specific compression (e.g., resizing screenshots before emailing):

// Using the Edit Image node instead of Code for image compression
// Edit Image node settings:
//   Operation: Resize
//   Width: 800
//   Height: (proportional)
//   Option > Quality: 75 (for JPEG)

Related: Flatten Deeply Nested API Responses · Use Docker Compose with Health Checks for n8n and PostgreSQL

Showcase builds

19 complete workflows from my own projects, each with its n8n workflow JSON to import. Showcase entries link the file at the end of the article.

See the showcase builds

Keep reading

190 entries grouped by topic, from first workflow to queue mode. Free, no signup.

Browse the encyclopedia

Need it built?

I design, build and run n8n systems for clients. Every engagement starts with a $1,500 diagnostic audit, credited toward the build.

Book an introductory call