High performance (de)compression in an 8kB package
Why fflate?
fflate (short for fast flate) is the fastest, smallest, and most versatile pure JavaScript compression and decompression library in existence, handily beating pako, tiny-inflate, and UZIP.js in performance benchmarks while being multiple times more lightweight. Its compression ratios are often better than even the original Zlib C library. It includes support for DEFLATE, GZIP, and Zlib data. Data compressed by fflate can be decompressed by other tools, and vice versa.
In addition to the base decompression and compression APIs, fflate supports high-speed ZIP file archiving for an extra 3 kB. In fact, the compressor, in synchronous mode, compresses both more quickly and with a higher compression ratio than most compression software (even Info-ZIP, a C program), and in asynchronous mode it can utilize multiple threads to achieve over 3x the performance of any other utility.
pako
tiny-inflate
UZIP.js
fflate
Decompression performance
1x
Up to 40% slower
Up to 40% faster
Up to 40% faster
Compression performance
1x
N/A
Up to 5% faster
Up to 50% faster
Base bundle size (minified)
45.6kB
3kB (inflate only)
14.2kB
8kB (3kB for inflate only)
Compression support
✅
❌
✅
✅
Demo
If you'd like to try fflate for yourself without installing it, you can take a look at the browser demo. Since fflate is a pure JavaScript library, it works in both the browser and Node.js (see Browser support for more info).
Usage
Install fflate:
npm i fflate # or yarn add fflate, or pnpm add fflate
Import:
// I will assume that you use the following for the rest of this guideimport * as fflate from 'fflate';// However, you should import ONLY what you need to minimize bloat.// So, if you just need GZIP compression support:import { gzipSync } from 'fflate';// Woo! You just saved 20 kB off your bundle with one line.
If your environment doesn't support ES Modules (e.g. Node.js):
// Try to avoid this when using fflate in the browser, as it will import// all of fflate's components, even those that you aren't using.const fflate = require('fflate');
If you want to load from a CDN in the browser:
<!--You should use either UNPKG or jsDelivr (i.e. only one of the following)Note that tree shaking is completely unsupported from the CDN. If you wanta small build without build tools, please ask me and I will make one manuallywith only the features you need. This build is about 27kB, or 9kB gzipped.You may also want to specify the version, e.g. with fflate@0.4.8--><script src="https://unpkg.com/fflate"></script><script src="https://cdn.jsdelivr.net/npm/fflate/umd/index.js"></script><!-- Now, the global variable fflate contains the library --><!-- If you're going buildless but want ESM, import from Skypack --><script type="module"> import * as fflate from 'https://cdn.skypack.dev/fflate?min';</script>
If you are using Deno:
// Don't use the ?dts Skypack flag; it isn't necessary for Deno support// The @deno-types comment adds TypeScript typings// @deno-types="https://cdn.skypack.dev/fflate/lib/index.d.ts"import * as fflate from 'https://cdn.skypack.dev/fflate?min';
If your environment doesn't support bundling:
// Again, try to import just what you need// For the browser:import * as fflate from 'fflate/esm/browser.js';// If the standard ESM import fails on Node (i.e. older version):import * as fflate from 'fflate/esm';
And use:
// This is an ArrayBuffer of dataconst massiveFileBuf = await fetch('/aMassiveFile').then( res => res.arrayBuffer());// To use fflate, you need a Uint8Arrayconst massiveFile = new Uint8Array(massiveFileBuf);// Note that Node.js Buffers work just fine as well:// const massiveFile = require('fs').readFileSync('aMassiveFile.txt');// Higher level means lower performance but better compression// The level ranges from 0 (no compression) to 9 (max compression)// The default level is 6const notSoMassive = fflate.zlibSync(massiveFile, { level: 9 });const massiveAgain = fflate.unzlibSync(notSoMassive);const gzipped = fflate.gzipSync
fflate can autodetect a compressed file's format as well:
const compressed = new Uint8Array( await fetch('/GZIPorZLIBorDEFLATE').then(res => res.arrayBuffer()));// Above example with Node.js Buffers:// Buffer.from('H4sIAAAAAAAAE8tIzcnJBwCGphA2BQAAAA==', 'base64');const decompressed = fflate.decompressSync(compressed);
Using strings is easy with fflate's string conversion API:
const buf = fflate.strToU8('Hello world!');// The default compression method is gzip// Increasing mem may increase performance at the cost of memory// The mem ranges from 0 to 12, where 4 is the defaultconst compressed = fflate.compressSync(buf, { level: 6, mem: 8 });// When you need to decompress:const decompressed = fflate.decompressSync(compressed);const origText = fflate.strFromU8(decompressed);console.log(origText); // Hello world!
If you need to use an (albeit inefficient) binary string, you can set the second argument to true.
const buf = fflate.strToU8('Hello world!');// The second argument, latin1, is a boolean that indicates that the data// is not Unicode but rather should be encoded and decoded as Latin-1.// This is useful for creating a string from binary data that isn't// necessarily valid UTF-8. However, binary strings are incredibly// inefficient and tend to double file size, so they're not recommended.const compressedString = fflate.strFromU8( fflate.compressSync(buf), true);const decompressed = fflate.decompressSync( fflate.strToU8(compressedString, true));const origText = fflate.strFromU8(decompressed);console.log(origText); // Hello world!
You can use streams as well to incrementally add data to be compressed or decompressed:
// This example uses synchronous streams, but for the best experience// you'll definitely want to use asynchronous streams.let outStr = '';const gzipStream = new fflate.Gzip({ level: 9 }, (chunk, isLast) => { // accumulate in an inefficient binary string (just an example) outStr += fflate.strFromU8(chunk, true);});// You can also attach the data handler separately if you don't want to// do so in the constructor.gzipStream.ondata = (chunk, final) => { ... }// Since this is synchronous, all errors will be thrown by stream.push()gzipStream.push(chunk1);gzipStream.push(chunk2);...
You can create multi-file ZIP archives easily as well. Note that by default, compression is enabled for all files, which is not useful when ZIPping many PNGs, JPEGs, PDFs, etc. because those formats are already compressed. You should either override the level on a per-file basis or globally to avoid wasting resources.
// Note that the asynchronous version (see below) runs in parallel and// is *much* (up to 3x) faster for larger archives.const zipped = fflate.zipSync({ // Directories can be nested structures, as in an actual filesystem 'dir1': { 'nested': { // You can use Unicode in filenames '你好.txt': std('Hey there!') }, // You can also manually write out a directory path 'other/tmp.txt': new Uint8Array([97, 98, 99, 100]) }, // You can also provide compression options 'myImageData.bmp': [aMassiveFile, { level: 9, mem: 12, // ZIP-specific: mtime works here too, defaults to current time
If you need extremely high performance or custom ZIP compression formats, you can use the highly-extensible ZIP streams. They take streams as both input and output. You can even use custom compression/decompression algorithms from other libraries, as long as they are defined in the ZIP spec (see section 4.4.5). If you'd like more info on using custom compressors, feel free to ask.
// ZIP object// Can also specify zip.ondata outside of the constructorconst zip = new fflate.Zip((err, dat, final) => { if (!err) { // output of the streams console.log(dat, final); }});const helloTxt = new fflate.ZipDeflate('hello.txt', { level: 9});// Always add streams to ZIP archives before pushing to those streamszip.add(helloTxt);helloTxt.push(chunk1);// Last chunkhelloTxt.push(chunk2, true);// ZipPassThrough is like ZipDeflate with level 0, but allows for tree shaking
As you may have guessed, there is an asynchronous version of every method as well. Unlike most libraries, this will cause the compression or decompression run in a separate thread entirely and automatically by using Web (or Node) Workers (as of now, Deno is unsupported). This means that the processing will not block the main thread at all.
Note that there is a significant initial overhead to using workers of about 70ms, so it's best to avoid the asynchronous API unless necessary. However, if you're compressing multiple large files at once, or the synchronous API causes the main thread to hang for too long, the callback APIs are an order of magnitude better.
import { gzip, zlib, AsyncGzip, zip, unzip, strFromU8, Zip, AsyncZipDeflate, Unzip, AsyncUnzipInflate} from 'fflate';// Workers will work in almost any browser (even IE11!)// However, they fail below Node v12 without the --experimental-worker// CLI flag, and will fail entirely on Node below v10.// All of the async APIs use a node-style callback as so:const terminate = gzip(aMassiveFile, (err, data) => { if (err) { // The compressed data was likely corrupt, so we have to handle // the error. return; } // Use data however you like console.log(data.length);});if (needToCancel) { // The return value of any of the asynchronous APIs is a function that, // when called, will immediately cancel the operation. The callback // will not be called.
See the documentation for more detailed information about the API.
Bundle size estimates
Since fflate uses ES Modules, this table should give you a general idea of fflate's bundle size for the features you need. The maximum bundle size that is possible with fflate is about 27kB if you use every single feature, but feature parity with pako is only around 10kB (as opposed to 45kB from pako). If your bundle size increases dramatically after adding fflate, please create an issue.
Feature
Bundle size (minified)
Nearest competitor
Decompression
3kB
tiny-inflate
Compression
5kB
UZIP.js, 184% larger
Async decompression
4kB (1kB + raw decompression)
N/A
Async compression
6kB (1kB + raw compression)
N/A
ZIP decompression
5kB (2kB + raw decompression)
UZIP.js, 184% larger
ZIP compression
7kB (2kB + raw compression)
UZIP.js, 103% larger
GZIP/Zlib decompression
4kB (1kB + raw decompression)
, 1040% larger
What makes fflate so fast?
Many JavaScript compression/decompression libraries exist. However, the most popular one, pako, is merely a clone of Zlib rewritten nearly line-for-line in JavaScript. Although it is by no means poorly made, pako doesn't recognize the many differences between JavaScript and C, and therefore is suboptimal for performance. Moreover, even when minified, the library is 45 kB; it may not seem like much, but for anyone concerned with optimizing bundle size (especially library authors), it's more weight than necessary.
Note that there exist some small libraries like tiny-inflate for solely decompression, and with a minified size of 3 kB, it can be appealing; however, its performance is lackluster, typically 40% worse than pako in my tests.
UZIP.js is both faster (by up to 40%) and smaller (14 kB minified) than pako, and it contains a variety of innovations that make it excellent for both performance and compression ratio. However, the developer made a variety of tiny mistakes and inefficient design choices that make it imperfect. Moreover, it does not support GZIP or Zlib data directly; one must remove the headers manually to use UZIP.js.
So what makes fflate different? It takes the brilliant innovations of UZIP.js and optimizes them while adding direct support for GZIP and Zlib data. And unlike all of the above libraries, it uses ES Modules to allow for partial builds through tree shaking, meaning that it can rival even tiny-inflate in size while maintaining excellent performance. The end result is a library that, in total, weighs 8kB minified for the core build (3kB for decompression only and 5kB for compression only), is about 15% faster than UZIP.js or up to 60% faster than pako, and achieves the same or better compression ratio than the rest.
If you're willing to have 160 kB of extra weight and much less browser support, you could theoretically achieve more performance than fflate with a WASM build of Zlib like wasm-flate. However, per some tests I conducted, the WASM interpreters of major browsers are not fast enough as of December 2020 for wasm-flate to be useful: fflate is around 2x faster.
Before you decide that fflate is the end-all compression library, you should note that JavaScript simply cannot rival the performance of a native program. If you're only using Node.js, it's probably better to use the native Zlib bindings, which tend to offer the best performance. Though note that even against Zlib, fflate is only around 30% slower in decompression and 10% slower in compression, and can still achieve better compression ratios!
Browser support
fflate makes heavy use of typed arrays (Uint8Array, Uint16Array, etc.). Typed arrays can be polyfilled at the cost of performance, but the most recent browser that doesn't support them is from 2011, so I wouldn't bother.
The asynchronous APIs also use Worker, which is not supported in a few browsers (however, the vast majority of browsers that support typed arrays support Worker).
Other than that, fflate is completely ES3, meaning you probably won't even need a bundler to use it.
Testing
You can validate the performance of fflate with npm/yarn/pnpmtest. It validates that the module is working as expected, ensures the outputs are no more than 5% larger than competitors at max compression, and outputs performance metrics to test/results.
Note that the time it takes for the CLI to show the completion of each test is not representative of the time each package took, so please check the JSON output if you want accurate measurements.
License
This software is MIT Licensed, with special exemptions for projects
and organizations as noted below:
SheetJS is exempt from MIT licensing and may
license any source code from this software under the BSD Zero Clause License
Thread/Worker safe
✅
✅
❌
✅
ZIP support
❌
❌
✅
✅
Streaming support
✅
❌
❌
✅
GZIP/Zlib support
✅
❌
❌
✅
Supports files up to 4GB
✅
❌
❌
✅
Doesn't hang on error
✅
❌
❌
✅
Multi-thread/Asynchronous
❌
❌
❌
✅
Streaming ZIP support
❌
❌
❌
✅
Uses ES Modules
❌
❌
❌
✅
(massiveFile, {
// GZIP-specific: the filename to use when decompressed
filename: 'aMassiveFile.txt',
// GZIP-specific: the modification time. Can be a Date, date string,
// or Unix timestamp
mtime: '9/1/16 2:00 PM'
});
// You should mark the last chunk by using true in the second argument
// In addition to being necessary for the stream to work properly, this
// will also set the isLast parameter in the handler to true.
gzipStream.push(lastChunk, true);
console.log(outStr); // The compressed binary string is now available
// The options parameter for compression streams is optional; you can
// provide one parameter (the handler) or none at all if you set
// deflateStream.ondata later.
const deflateStream = new fflate.Deflate((chunk, final) => {
console.log(chunk, final);
});
// If you want to create a stream from strings, use EncodeUTF8
const utfEncode = new fflate.EncodeUTF8((data, final) => {
// Chaining streams together is done by pushing to the
// next stream in the handler for the previous stream
deflateStream.push(data, final);
});
utfEncode.push('Hello'.repeat(1000));
utfEncode.push(' '.repeat(100));
utfEncode.push('world!'.repeat(10), true);
// The deflateStream has logged the compressed data