REST API JSON Performance: Payload Optimization & Compression
A guide to slashing API latency, reducing AWS/Cloudflare egress bandwidth costs, and engineering sub-50ms JSON response pipelines.
1. Where Does JSON Payload Bloat Come From?
In high-throughput microservices handling millions of daily requests, payload inefficiencies accumulate rapidly into thousands of dollars in cloud egress bills and mobile network latency.
Pretty-printed JSON with 2 or 4-space indentation adds 35% to 50% dead byte weight.
Serializing entire database entity rows when the frontend only renders 3 columns.
Repeating descriptive 25-character key names across a 10,000-element array.
2. Benchmark: Raw JSON vs Gzip vs Brotli
| Payload Format | Payload Size | Size Reduction | Decompression Latency |
|---|---|---|---|
| Pretty-Printed JSON | 1,450 KB | Baseline (0%) | 0 ms |
| Minified JSON (No whitespace) | 820 KB | 43.4% smaller | 0 ms |
| Minified JSON + Gzip (Level 6) | 168 KB | 88.4% smaller | 1.8 ms |
| Minified JSON + Brotli (Level 5) | 132 KB | 90.9% smaller | 1.4 ms |
3. Implementing Sparse Fieldsets in Express / Fastify
Allow clients to request specific keys using the ?fields= query parameter:
import express from "express";
const app = express();
function pickFields(obj, fields) {
if (!fields) return obj;
const allowed = new Set(fields.split(","));
return Object.fromEntries(
Object.entries(obj).filter(([key]) => allowed.has(key))
);
}
app.get("/api/users", (req, res) => {
const users = getFullUserDatabaseRecords();
const fields = req.query.fields; // e.g. "id,username,email"
res.json(users.map(u => pickFields(u, fields)));
});