Introduction
Need to compress JavaScript strings for localStorage or URL parameters? This LZ-String compressor uses the lz-string library by pieroxy to compress and decompress text with four encoding options: default (UTF-8), UTF-16, Base64, and URI-safe. Developers can reduce string sizes for client-side storage, URL parameters, or JSON payloads, all running entirely in the browser.
What this tool does
- Compresses text using the LZ-String algorithm with four output encodings: default, UTF-16, Base64, and URI-safe.
- Decompresses LZ-String compressed data back to the original text using the matching encoding.
- Uses the lz-string library (version 1.5.0), which was designed specifically for JavaScript string storage.
- Lets you switch encoding formats to match your use case: localStorage, URL parameters, or JSON transport.
- Runs entirely client-side with no network requests.
How this tool works
Select Compress or Decompress mode using the buttons in the settings panel. Choose an output encoding from the dropdown: Default (raw UTF-8, smallest but may contain invalid characters), UTF-16 (safe for localStorage on all browsers), Base64 (safe for JSON and HTTP transport), or URI-safe (safe for URL parameters without encoding). Paste your text into the input field. The compressed or decompressed result appears instantly in the output field. The Swap button toggles between modes and moves the output into the input field. If you paste invalid compressed data in Decompress mode, the tool displays an error message.
How LZ-String compression works
LZ-String is a JavaScript compression library created by pieroxy (Pierre Rosset) and first released in 2013. It was designed to fulfill the need of storing large amounts of data in localStorage, which is typically limited to 5 MB on mobile devices. The algorithm is based on the Lempel-Ziv (LZ) family of dictionary-based compression methods. It works by building a dictionary of previously seen strings as it scans the input, replacing repeated sequences with references to earlier occurrences. Unlike standard LZ implementations that operate on bytes, LZ-String works with 16-bit-wide tokens to match JavaScript's internal UTF-16 string representation. The library provides multiple encoding options because the raw compress function produces strings that use all 16 bits per character, which are not valid UTF-16 and cannot be safely stored in localStorage on all browsers. The compressToUTF16 function uses only 15 bits per character to produce valid UTF-16 strings (6.66 percent larger than raw). The compressToBase64 function uses 6 bits per character to produce ASCII strings (166 percent larger than raw but still useful for JSON). The compressToEncodedURIComponent function produces URI-safe ASCII strings for use in URL parameters. According to npm statistics, lz-string has over 57 million weekly downloads and is used by more than 1,283 packages, making it one of the most popular compression libraries in the JavaScript ecosystem.
How to use this tool
- Select Compress or Decompress mode.
- Choose an encoding: Default for smallest output, UTF-16 for localStorage, Base64 for JSON or HTTP, or URI-safe for URL parameters.
- Paste your text in the input field for compression, or paste compressed data for decompression.
- Read the result in the output field. Use Copy result to copy it.
- Use Swap to toggle between compression and decompression.
Real-world examples
Compressing JSON for localStorage
Input: a 2 KB JSON string with repeated keys and values. Using UTF-16 encoding, the compressed output is typically 40-60 percent smaller, allowing you to store more data within the 5 MB localStorage limit. Use compressToUTF16 because it produces valid UTF-16 strings that all browsers can store safely.
Compressing data for URL parameters
Input: a long query string. Using URI-safe encoding, the compressed output can be placed directly in a URL without additional encoding. This saves bandwidth when passing data between pages or to a server.
Compressing for API transport
Input: a large JSON payload. Using Base64 encoding, the compressed output is an ASCII string that can be safely embedded in JSON, sent over HTTP, or stored in a database. The output is larger than raw compression but safe for all text-based transport.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| LZ-String (default) | LZ-based, 16-bit tokens | localStorage on webkit browsers, smallest output |
| LZ-String (UTF-16) | LZ-based, 15-bit tokens | localStorage on all browsers, 6.66% larger than default |
| LZ-String (Base64) | LZ-based, 6-bit tokens | JSON, HTTP transport, 166% larger than default |
| gzip (DEFLATE) | LZ77 + Huffman coding | Better compression ratio for larger data, but binary output |
Limitations or considerations
LZ-String is optimized for JavaScript string storage, not for general-purpose file compression. For large texts (over 100 KB), gzip or brotli typically achieves better compression ratios. The default compress function produces strings with invalid UTF-16 characters that may not be storable in localStorage on non-webkit browsers. Always use compressToUTF16 for cross-browser localStorage. The Base64 and URI-safe encodings are significantly larger than the raw compressed output. LZ-String is a compression library, not an encryption tool, and provides no confidentiality.
Frequently asked questions
Which encoding should I use?
Use Default (raw) if you only need to store in localStorage on webkit browsers (Chrome, Safari). Use UTF-16 for localStorage on all browsers. Use Base64 for JSON, HTTP transport, or database storage. Use URI-safe for URL parameters.
How much compression can I expect?
For text with repeated patterns (such as JSON with repeated keys), LZ-String typically achieves 40-60 percent size reduction with the default encoding. Base64 encoding reduces this benefit because it uses only 6 bits per character. For highly repetitive text, compression can be even better.
Is LZ-String compatible with gzip?
No. LZ-String uses its own algorithm that is not compatible with gzip, DEFLATE, or any other standard compression format. Data compressed with LZ-String can only be decompressed with LZ-String using the matching encoding.
Can I use LZ-String on the server (Node.js)?
Yes. The lz-string npm package works in Node.js as well as in the browser. It has a command-line interface for compressing and decompressing files. However, for server-side compression of larger data, consider using Node.js's built-in zlib module which implements gzip and DEFLATE.
Conclusion
This LZ-String compressor gives developers a browser-based tool for compressing and decompressing JavaScript strings with four encoding options. It is particularly useful for reducing localStorage usage, shortening URL parameters, and compressing JSON payloads. For larger data or general-purpose compression, gzip or brotli may achieve better ratios.