Comparing gzip and zstd Compression Speed and Size in Bun
In Bun, you can pass the same data to Bun.gzipSync() and Bun.zstdCompressSync(), then compare the compressed byte count and the time measured with Bun.nanoseconds(). That tells you numerically which of gzip and zstd fits your data. This helps when you compress API responses or log files to reduce transfer size or storage.
In my test environment, I compressed about 1 MB of JSON. With both at their default levels, zstd finished in about one fifth of gzip's time, and its output was only about 3% larger. However, comparing default levels alone does not tell you which one is better for you.
The test environment was Ubuntu 24.04 (x86_64, 4 CPUs) with Bun 1.4.4. I only compared the functions built into Bun: Bun.gzipSync and Bun.zstdCompressSync, along with their decompression counterparts. I did not test brotli, node:zlib, CompressionStream, the gzip or zstd command-line tools, or how browsers handle these formats when returned as HTTP responses. I read the official documentation in its source files on GitHub at the tag used for testing (bun-v1.4.4), and the links point there.
Functions for calling gzip and zstd in Bun
Use Bun.gzipSync() and Bun.gunzipSync() to compress and decompress gzip data. The level option accepts -1 to 9, and the default is 6. Level 1 is the fastest, and level 9 produces the smallest output.
For zstd (Zstandard), use Bun.zstdCompressSync() and Bun.zstdDecompressSync(). The code example in the official documentation says level ranges from 1 to 22, with a default of 3. In my test environment, passing 0, 23, or -1 caused an error every time.
zstd level 0: TypeError Compression level must be between 1 and 22
zstd level 23: TypeError Compression level must be between 1 and 22
zstd level -1: TypeError Compression level must be between 1 and 22
For gzip, level: -1 means "use the default level," but zstd does not accept it. The level ranges and defaults also differ, so the same number does not mean the same thing in both.
The two formats are not compatible with each other. The first 4 bytes of compressed output were 1f8b0800 for gzip and 28b52ffd for zstd. Passing zstd data to Bun.gunzipSync() failed with incorrect header check, and passing gzip data to Bun.zstdDecompressSync() failed with Decompression failed: InvalidZstdData. The code that decompresses the data needs to know which format it is.
The benchmark script
I wrote bench.ts, which compresses the same data with gzip levels 1, 6, and 9, and zstd levels 1, 3, 9, and 19. For each method, it outputs:
- Compressed size in bytes
- Compressed size as a percentage of the original
- Compression time
- Decompression time
Each timing does 3 warm-up runs, then 21 measured runs, and reports the median. The warm-up runs are not included in the timing. Before measuring, the script also checks that decompressing gives back the original data.
import { makeJson, makeLog, makeRandom } from "./data.ts";
// Runs one method 3 times as warm-up, then RUNS times for timing, and returns the median time in milliseconds
const RUNS = 21;
function median(fn: () => unknown): number {
for (let i = 0; i < 3; i++) fn();
const times: number[] = [];
for (let i = 0; i < RUNS; i++) {
const start = Bun.nanoseconds();
fn();
times.push((Bun.nanoseconds() - start) / 1e6);
}
times.sort((a, b) => a - b);
return times[Math.floor(RUNS / 2)];
}
type Method = {
label: string;
compress: (d: Uint8Array) => Uint8Array;
decompress: (c: Uint8Array) => Uint8Array;
};
const gzip = (level: number): Method => ({
label: `gzip level ${level}`,
compress: (d) => Bun.gzipSync(d, { level: level as 1 }),
decompress: (c) => Bun.gunzipSync(c),
});
const zstd = (level: number): Method => ({
label: `zstd level ${level}`,
compress: (d) => Bun.zstdCompressSync(d, { level }),
decompress: (c) => Bun.zstdDecompressSync(c),
});
const methods = [gzip(1), gzip(6), gzip(9), zstd(1), zstd(3), zstd(9), zstd(19)];
const datasets = { json: makeJson(), log: makeLog(), random: makeRandom(), "small-json": makeJson(10) };
for (const [name, data] of Object.entries(datasets)) {
console.log(`\n## ${name}: ${data.length.toLocaleString()} バイト`);
console.log("方法 | 圧縮後(バイト) | 元に対する割合 | 圧縮(ms) | 展開(ms)");
for (const m of methods) {
const c = m.compress(data);
// Also check that decompressing returns the original data
if (!Buffer.from(m.decompress(c)).equals(Buffer.from(data))) throw new Error(`${m.label}: 元に戻らない`);
const ct = median(() => m.compress(data));
const dt = median(() => m.decompress(c));
const pct = ((c.length / data.length) * 100).toFixed(1);
console.log(`${m.label} | ${c.length.toLocaleString()} | ${pct}% | ${ct.toFixed(2)} | ${dt.toFixed(2)}`);
}
}
In the type definitions, Bun.gzipSync()'s level is a union of the individual numbers from -1 to 9. The script uses as 1 so that it can pass the level from a variable.
I generated the test data with functions in a separate data.ts file (makeJson and others). There are four datasets. The random seed is fixed, so the data is the same on every run. To test your own data, replace the entries in datasets with byte arrays loaded from files, such as await Bun.file("data.json").bytes().
- JSON resembling an API response (7,000 products, 988,031 bytes)
- Text resembling a web server access log (9,000 lines, 1,051,444 bytes)
- Random bytes (1,048,576 bytes), standing in for data that does not shrink, such as already-compressed images or videos
- Small JSON with only 10 products (1,347 bytes)
I ran bun bench.ts three times. The compressed sizes were identical each time, and the timings varied a little between runs. The timings in the tables below are the median of the three runs.
For JSON and logs, zstd at the default level is faster
These are the results for the roughly 1 MB JSON.
| Method | Compressed (bytes) | Ratio to original | Compress (ms) | Decompress (ms) |
|---|---|---|---|---|
| gzip level 1 | 278,524 | 28.2% | 2.60 | 1.28 |
| gzip level 6 (default) | 174,929 | 17.7% | 13.58 | 1.07 |
| gzip level 9 | 164,181 | 16.6% | 28.70 | 1.06 |
| zstd level 1 | 178,273 | 18.0% | 2.15 | 0.94 |
| zstd level 3 (default) | 180,036 | 18.2% | 2.61 | 0.90 |
| zstd level 9 | 154,626 | 15.6% | 14.79 | 0.69 |
| zstd level 19 | 136,207 | 13.8% | 419.51 | 0.81 |
Comparing default levels, zstd (2.61 ms) finished in about one fifth of gzip's time (13.58 ms). Its compressed size was about 3% larger.
When you compare methods that take similar amounts of time, the result changes. zstd level 9 took 14.79 ms, slightly longer than gzip's default (level 6, 13.58 ms), and its output was about 12% smaller than gzip's. It was both faster and smaller than gzip level 9. zstd level 1 was faster than gzip level 1, and its output was about two thirds the size. For this data, zstd level 3 was slightly larger than level 1.
The access log showed the same pattern.
| Method | Compressed (bytes) | Ratio to original | Compress (ms) | Decompress (ms) |
|---|---|---|---|---|
| gzip level 1 | 159,847 | 15.2% | 1.61 | 0.70 |
| gzip level 6 (default) | 98,550 | 9.4% | 6.40 | 0.55 |
| gzip level 9 | 92,430 | 8.8% | 13.94 | 0.58 |
| zstd level 1 | 116,257 | 11.1% | 1.48 | 0.65 |
| zstd level 3 (default) | 115,348 | 11.0% | 2.11 | 0.66 |
| zstd level 9 | 89,574 | 8.5% | 9.93 | 0.49 |
| zstd level 19 | 76,094 | 7.2% | 402.44 | 0.52 |
For the log, zstd at the default level finished in about one third of gzip's time, but its output was about 17% larger. The size gap is wider than for JSON. zstd level 9 was faster than gzip level 9 and produced slightly smaller output.
The following scatter plot shows compression time on the horizontal axis and compressed size on the vertical axis, for both datasets. Methods closer to the bottom left are faster and smaller.

zstd level 19 produced the smallest output for both datasets, but it took more than 400 ms to compress. That is about 30 times gzip's default-level time for JSON and about 60 times for the log. If you compress on every request, you pay that cost every time.
Decompression times ranged from about 0.5 to 1.3 ms for all methods, so the differences were much smaller than for compression. For JSON, zstd decompressed slightly faster. For the log, gzip was slightly faster at the default levels.
Don't decide based only on a comparison at default levels. For JSON, zstd at level 9, which takes about as long as gzip's default, produced smaller output than gzip. Compare your own data by fixing either the time or the size and checking the other.
Incompressible and small data show little difference
The random bytes barely shrank with any method.
| Method | Compressed (bytes) | Ratio to original | Compress (ms) | Decompress (ms) |
|---|---|---|---|---|
| gzip level 1 | 1,105,811 | 105.5% | 12.87 | 3.47 |
| gzip level 6 (default) | 1,048,917 | 100.0% | 22.31 | 0.13 |
| zstd level 3 (default) | 1,048,609 | 100.0% | 0.26 | 0.06 |
(This table is an excerpt. The results for the other levels were also close to the original size.)
For incompressible data, gzip took about 22 ms at its default level, and at level 1 the output was 5.5% larger than the input. zstd at its default level finished in about 0.3 ms.
For the small JSON with only 10 products (1,347 bytes), every method except gzip level 1 produced 462 to 485 bytes. Compression time was 0.02 ms or less for every method except zstd level 19, which took 0.15 ms. At this size, the choice makes little practical difference.
For heavy compression, use the async zstdCompress
Bun.zstdCompressSync() blocks other work until compression finishes. zstd also has an asynchronous version, Bun.zstdCompress(), which you can await. I compressed the same JSON at level 19 and counted how many times a 10 ms timer fired during compression, using a script called async.ts.
zstdCompress(非同期): 136207 バイト、555ms、その間に 10ms おきのタイマーが 54 回動いた
同期版と同じ結果か: true
zstdCompressSync(同期): 448ms、その間にタイマーが 1 回動いた
With the async version, the timer kept firing during compression. With the sync version, the timer barely fired until compression finished. The output bytes were identical in both cases. In this single run, the async version took longer to compress (555 ms versus 448 ms). If you need to keep other work running during compression, use the async version. For gzip, the official documentation only lists the synchronous Bun.gzipSync().
Notes on using these results
According to the official documentation, Bun's fetch automatically decompresses responses compressed with gzip, deflate, brotli, and zstd. For how to return gzip responses from Bun.serve, see how to compress Bun.serve responses with gzip. If you return zstd responses, the client must be able to decompress zstd. As I confirmed in the first section, Bun.gunzipSync() cannot decompress zstd data.
The numbers here apply to this data and this environment. As the JSON, log, and random-byte results show, the data's content changes both the speed difference and how much it shrinks. To produce the same tables with your own data, load the responses or files you actually serve or store into datasets in bench.ts.





