Compression

Gzip, Brotli, and LZ4 compression adapters for Keyv.

Compression runs on the serialized string, after JSON/SuperJSON/msgpackr and before optional encryption.

import Keyv from "keyv";
import KeyvGzip from "@keyv/compress-gzip";

const keyv = new Keyv({ compression: new KeyvGzip() });

Table of Contents

Official adapters

Package Algorithm
@keyv/compress-gzip Gzip (pako)
@keyv/compress-brotli Brotli
@keyv/compress-lz4 LZ4

Pick gzip for portability, brotli for smaller payloads at higher CPU, lz4 when you want faster (de)compress.

Custom adapter

interface KeyvCompressionAdapter {
	compress(value: string): Promise<string>;
	decompress(value: string): Promise<string>;
}

Test with @keyv/test-suite:

import { compressionTestSuite } from "@keyv/test-suite";
import { it } from "vitest";
import KeyvGzip from "@keyv/compress-gzip";

compressionTestSuite(it, new KeyvGzip());

Serialization must stay enabled (the default). Disabling serialization bypasses compression regardless of the original value type.

Edit this page