Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readNode

Compress data with zstd in node:zlib

Compare gzip and Zstandard on one JSON fixture, verify the decoded bytes, and stream an export with node:zlib.

Your JSON export takes seconds to upload, and the receiver already accepts gzip. Before replacing gzip, compare Zstandard on a sample of the actual export and check that the result decodes to the same bytes.

Compare both formats on one input

node:zlib adds experimental Zstandard support on the Node 22 line in 22.15. Save this example as compare.mjs and run node compare.mjs to compare the defaults. This one creates repeated events with varying IDs and user IDs. Use a scrubbed export for the decision about your app.

import { gzipSync, zstdCompressSync, zstdDecompressSync } from 'node:zlib'
import assert from 'node:assert/strict'

const events = Array.from({ length: 50_000 }, (_, id) => ({
  id,
  user: (id * 37) % 997,
  type: id % 3 === 0 ? 'open' : 'save',
}))
const input = Buffer.from(JSON.stringify(events))

const gzip = gzipSync(input)
const zstd = zstdCompressSync(input)

console.log({ input: input.length, gzip: gzip.length, zstd: zstd.length })
assert.deepEqual(zstdDecompressSync(zstd), input)

The console prints the byte counts for the same input, and the assertion checks that decompressing the Zstandard frame recovers the export.

This compares default settings, which can do different amounts of work. To pick a setting, measure compression time and size across representative payloads at more than one level. Include the decoder's cost if the receiving service unpacks many exports. This fixture repeats field names and event types, so its ratios can differ from an export with large free-text fields.

Stream a large file

The synchronous functions suit a quick command-line experiment. In a server handler they block the JavaScript thread while compressing. For a large file, use a stream so you can write the result without holding both buffers in memory. Save the fixture to events.json or use a file of your own. Save the stream example as compress.mjs and run node compress.mjs from the same directory.

import { createReadStream, createWriteStream } from 'node:fs'
import { pipeline } from 'node:stream/promises'
import { createZstdCompress } from 'node:zlib'

await pipeline(
  createReadStream('events.json'),
  createZstdCompress(),
  createWriteStream('events.json.zst'),
)

The pipeline writes a Zstandard frame to events.json.zst and rejects if any stream fails.

Use a distinct output path so a failed write cannot damage the source file. If another job watches the destination, write to a temporary filename and rename it after the pipeline resolves. Keep gzip for any receiver that accepts only gzip, and update the decoder side before changing what the producer sends.