A tiny, text-based LZW implementation for JS with built-in optimizations. Useful for IndexDB storage with minimal overhead.
- ✅ Uses native TextEncoder/TextDecoder and Uint8Arrays which makes it input-agnostic
- ✅ Full support for unicode/emoji character range
- ✅ Utilizes a dynamic bit size optimization to squeeze out a few extra bytes
npm i lzw-lite
import {compress, decompress} from "lzw-lite";
/** Convert string -> Uint8Array to store in IndexDB */
const mystringToCompress = "insert string here";
const compressed: Uint8Array = compress(mystringToCompress);
/** Convert Uint8Array -> string to get original value back */
const myOriginalString = decompress(compressed);
For optimal bundle size, lzw-lite
is compiled with target ESNext
. The package.json
exposes both a type module
(preffered) and type UMD
export. If your target does not support the given ESM
features, you will have to recompile/polyfill with your own bundler.
- Dictionary resets for large files
- For large files (~1GB+), there is a chance that the dictionary will exceed
2 ** 31
which will cause an overflow in bit-shift operations. - TLDR: do not use the library with large files as it has not been optimized for this use-case.
Eventually, the CompressionStream and DecompressionStream API from Google will hopefully land in most browsers which will provide a native GZIP library in the browser.
For now, the API's are only avaliable on latest Chrome versions and there are no plans from other browsers (so far) to support them.
https://github.com/pieroxy/lz-string/blob/master/libs/lz-string.min.js (4.61 KB min)