fzstd
High performance Zstandard decompression in a pure JavaScript, 8kB package
Usage
Import:
import * as fzstd from 'fzstd';
If your environment doesn't support ES Modules (e.g. Node.js):
const fzstd = require('fzstd');
If you want to load from a CDN in the browser:
<script src="https://unpkg.com/fzstd"></script>
<script src="https://cdn.jsdelivr.net/npm/fzstd/umd/index.js"></script>
<script type="module">
import * as fzstd from 'https://cdn.skypack.dev/fzstd?min';
</script>
If you are using Deno:
import * as fzstd from 'https://cdn.skypack.dev/fzstd?min';
And use:
const compressedBuf = await fetch('/compressedData.zst').then(
res => res.arrayBuffer()
);
const compressed = new Uint8Array(compressedBuf);
const decompressed = fzstd.decompress(compressed);
const outBuf = new Uint8Array(100000);
fzstd.decompress(compressed, outBuf);
You can also use data streams to minimize memory usage while decompressing.
let outChunks = [];
const stream = new fzstd.Decompress((chunk, isLast) => {
outChunks.push(chunk);
if (isLast) {
console.log('Output chunks:', outChunks);
}
});
stream.ondata = (chunk, final) => { ... }
stream.push(chunk1);
stream.push(chunk2);
...
stream.push(chunkLast, true);
Considerations
Unlike my Zlib implementation fflate
, WebAssembly ports of Zstandard are usually significantly (40-50%) faster than fzstd
. However, they fail to decompress most archives without the decompressed size provided in advance. Moreover, they do not support streaming and thereby allocate a large amount of memory that cannot be freed. Lastly, fzstd
is absolutely tiny: at 8kB minified and 3.8kB after gzipping, it's much smaller than most WASM implementations.
Please note that unlike the reference implementation, fzstd
only supports a maximum backreference distance of 225 bytes. If you need to decompress files with an "ultra" compression level (20 or greater) AND if your files can be above 32MB decompressed, fzstd
might fail to decompress properly. Consider using a WebAssembly port for files this large (though this may be difficult if you don't know the decompressed size).