Skip to main content

Import

What it does

CompressionUtils provides static methods for LZ-based string compression and decompression. It supports multiple output formats — plain string, Base64, and Uint8Array — and includes convenience methods for compressing and decompressing JSON objects directly.
The compression algorithm is deterministic and synchronous. It works in both browser and Node.js environments without native dependencies.

Methods

compress(data, options?)

Compresses a string using LZ-based compression and returns a compressed string.

decompress(compressed)

Decompresses a previously compressed string.

compressToBase64(data)

Compresses a string and returns the result as a Base64-encoded string. Useful for URL parameters or storage in text-only fields.

decompressFromBase64(base64)

Decompresses a Base64-encoded compressed string.

compressToUint8Array(data)

Compresses a string and returns the result as a Uint8Array. Ideal for binary transport (WebSocket, IndexedDB).

decompressFromUint8Array(compressed)

Decompresses a Uint8Array back to the original string.

estimateCompressionRatio(data)

Returns a number between 0 and 1 representing the estimated compression ratio. A value of 0.3 means the compressed output is roughly 30% of the original size.

compressJSON(obj)

Serializes an object to JSON and compresses the result in one step.

decompressJSON<T>(compressed)

Decompresses a string and parses the result as JSON, returning a typed object.

Examples

Compress state for URL sharing

Compress JSON for localStorage

Estimate savings before compressing

Use compressToBase64 when the compressed output needs to travel through text-only channels (URLs, JSON fields, cookies).
Compression is most effective on repetitive or structured text. Small strings (under ~100 characters) may produce output that is larger than the input.