LZ4
LZ4
Byte-aligned matches, no entropy coder. Built for speed.
- Ratio
- 14% of the input, on text
- Checksum
- XXH32
- Standard
- LZ4 Block Format (Yann Collet)
- Used by
- ZFS, Linux, game assets
Containers
- frame
- LZ4 frame.lz4 · magic 04224d18 · XXH32
- 42 bytes · 04224d186470b917000000df…reads back · blockSize 4194304, independent true, checksum true, frames 1
- legacy
- LZ4 legacy frame.lz4 · magic 02214c18 · no checksum
- 31 bytes · 02214c1817000000df68656c…reads back · blocks 1
Options
Access
- Import
import { lz4 } from "@agntn/compressions/lz4" - CLI
compressions compress lz4 notes.txt --container frame > file.lz4 - Tryplayground with the sample above
LZ4 skips the entropy coding entirely. Every sequence is a token byte, the literals as they are, a two-byte offset and a length. No bits to shuffle, so it reads at close to memory speed, and gives up some size for it.
compress("lz4", "hello world!"); // 31 bytes, frame
compress("lz4", "hello world!", { container: "legacy" }); // 21 bytes
Frame
The default. Magic 04 22 4d 18, a descriptor with its own XXH32 byte, blocks, an end mark, and the XXH32 of the content when checksum is on, which it is by default. Turning it off saves four bytes:
compress("lz4", "hello world!", { checksum: false }).length; // 27
The writer uses 4 MiB independent blocks, the same as lz4 with defaults. The reader takes linked blocks too, block checksums, a stated content size, and frames in a row. A frame that needs a dictionary is an UnsupportedError.
Legacy
Magic 02 21 4c 18, then 8 MiB blocks each with its size, and nothing else. No checksum, no end mark. It's what lz4 -l writes and what the Linux kernel still boots from when it is built with LZ4.