QA
Beginner LevelNode JS

How do you compress data streams using gzip and brotli in node:zlib?

❓ Interview Prompt: Explain `node:zlib` module, Gzip vs Brotli compression, streaming data compression, and HTTP response compression.

The 'node:zlib' module provides compression and decompression algorithms:

•
Gzip ('zlib.createGzip()'): Fast, widely supported HTTP response compression.
•
Brotli ('zlib.createBrotliCompress()'): Modern algorithm delivering 20% higher compression ratios than Gzip.

Code Implementation Example

javascript
import zlib from 'node:zlib';
import { promisify } from 'node:util';

const gzip = promisify(zlib.gzip);

async function compressPayload(data) {
  const buffer = Buffer.from(JSON.stringify(data));
  const compressed = await gzip(buffer);
  console.log('Original: ${buffer.length}B -> Compressed: ${compressed.length}B');
  return compressed;
}
💡Compressing JSON payload buffer using node:zlib gzip.
🎯 Interview Key Takeaway

Use Brotli or Gzip stream compression in HTTP responses to reduce network transfer payload size.

#zlib#gzip#brotli#Compression#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?