Convert Figma logo to code with AI

pieroxy logolz-string

LZ-based compression algorithm for JavaScript

4,075
567
4,075
52

Top Related Projects

Base64 implementation for JavaScript

5,494

high speed zlib port to javascript, works in browser & node.js

1,122

compact zlib, deflate, inflate, zip library in JavaScript

2,207

High performance (de)compression in an 8kB package

Quick Overview

LZ-String is a JavaScript library for string compression and decompression. It implements the LZ-based compression algorithm to reduce the size of strings, making it useful for storing or transmitting large amounts of data in a compact format.

Pros

  • Efficient compression of strings, reducing data size significantly
  • Cross-platform compatibility (works in browsers and Node.js)
  • Multiple compression methods available for different use cases
  • Lightweight and easy to integrate into existing projects

Cons

  • Limited to string compression only, not suitable for binary data
  • May not be as effective for very short strings
  • Performance can be slower compared to native compression algorithms
  • Not actively maintained (last update was in 2021)

Code Examples

Compressing a string:

const LZString = require('lz-string');

const originalString = "Hello, world! This is a test string for compression.";
const compressed = LZString.compress(originalString);
console.log(compressed);

Decompressing a string:

const decompressed = LZString.decompress(compressed);
console.log(decompressed); // Output: "Hello, world! This is a test string for compression."

Using UTF-16 compression for better compatibility:

const compressedUTF16 = LZString.compressToUTF16(originalString);
const decompressedUTF16 = LZString.decompressFromUTF16(compressedUTF16);
console.log(decompressedUTF16); // Output: "Hello, world! This is a test string for compression."

Getting Started

  1. Install the library using npm:
npm install lz-string
  1. Import and use the library in your JavaScript code:
const LZString = require('lz-string');

const originalString = "Your string to compress";
const compressed = LZString.compress(originalString);
const decompressed = LZString.decompress(compressed);

console.log(decompressed === originalString); // Should output: true

For browser usage, include the script in your HTML:

<script src="https://cdn.jsdelivr.net/npm/lz-string@1.4.4/libs/lz-string.min.js"></script>

Then use it in your JavaScript code as shown above.

Competitor Comparisons

Base64 implementation for JavaScript

Pros of js-base64

  • More widely adopted and battle-tested
  • Supports a broader range of encoding/decoding options
  • Better documentation and examples

Cons of js-base64

  • Larger file size compared to lz-string
  • May be slower for large data sets
  • Less focused on compression

Code Comparison

js-base64:

import { Base64 } from 'js-base64';

const encoded = Base64.encode('Hello, World!');
const decoded = Base64.decode(encoded);

lz-string:

import LZString from 'lz-string';

const compressed = LZString.compress('Hello, World!');
const decompressed = LZString.decompress(compressed);

Both libraries offer simple APIs for encoding/compressing and decoding/decompressing strings. However, js-base64 focuses on Base64 encoding, while lz-string emphasizes string compression.

js-base64 is more suitable for general-purpose encoding tasks and interoperability with other Base64 systems. lz-string excels in scenarios where minimizing data size is crucial, such as client-side storage or data transfer over limited bandwidth connections.

Choose js-base64 if you need a versatile Base64 encoding solution with extensive options. Opt for lz-string if your primary goal is to reduce string size efficiently, especially for web applications dealing with large amounts of textual data.

5,494

high speed zlib port to javascript, works in browser & node.js

Pros of pako

  • Supports multiple compression algorithms (deflate, gzip, zlib)
  • Generally faster compression and decompression speeds
  • More actively maintained with regular updates

Cons of pako

  • Larger file size (around 45KB minified)
  • More complex API, potentially steeper learning curve
  • May be overkill for simple string compression tasks

Code Comparison

lz-string:

var compressed = LZString.compress("Hello, World!");
var decompressed = LZString.decompress(compressed);

pako:

var compressed = pako.deflate("Hello, World!");
var decompressed = pako.inflate(compressed, {to: 'string'});

Summary

lz-string is a lightweight library focused on string compression, making it ideal for simple use cases and smaller projects. It has a straightforward API but may not be as performant for larger datasets.

pako offers more comprehensive compression options and better performance, suitable for larger projects or those requiring specific compression algorithms. However, it comes with a larger file size and more complex usage.

Choose lz-string for simplicity and small file size, or pako for versatility and performance in more demanding applications.

1,122

compact zlib, deflate, inflate, zip library in JavaScript

Pros of zlib.js

  • Implements the standard zlib compression algorithm, offering better compatibility with other systems
  • Provides both compression and decompression functionality
  • Supports streaming for handling large data sets efficiently

Cons of zlib.js

  • Generally larger file size compared to lz-string
  • May have slower performance for small data sets
  • More complex API, potentially requiring more setup and configuration

Code Comparison

lz-string:

var compressed = LZString.compress("Hello, World!");
var decompressed = LZString.decompress(compressed);

zlib.js:

var compressed = pako.deflate("Hello, World!");
var decompressed = pako.inflate(compressed, {to: 'string'});

Summary

lz-string is a lightweight compression library focused on simplicity and small file sizes, making it ideal for client-side use and URL-safe string compression. zlib.js, on the other hand, offers a more robust implementation of the zlib algorithm, providing better compatibility with other systems and support for streaming operations. While zlib.js may have a larger file size and more complex API, it offers more features and flexibility for advanced compression needs. The choice between the two depends on the specific requirements of your project, such as file size constraints, compatibility needs, and the complexity of data being compressed.

2,207

High performance (de)compression in an 8kB package

Pros of fflate

  • Supports a wider range of compression algorithms (DEFLATE, GZIP, ZLIB)
  • Generally faster compression and decompression speeds
  • Smaller bundle size for browser usage

Cons of fflate

  • More complex API, potentially steeper learning curve
  • Less focused on string compression specifically
  • Newer project with potentially less community support

Code Comparison

fflate:

import { strToU8, strFromU8, zlibSync } from 'fflate';

const compressed = zlibSync(strToU8('Hello, World!'));
const decompressed = strFromU8(zlibSync(compressed, { to: 'string' }));

lz-string:

import LZString from 'lz-string';

const compressed = LZString.compress('Hello, World!');
const decompressed = LZString.decompress(compressed);

Summary

fflate offers more comprehensive compression options and better performance, making it suitable for a wider range of applications. However, lz-string provides a simpler API focused specifically on string compression, which may be preferable for simpler use cases or developers seeking a more straightforward implementation. The choice between the two depends on the specific requirements of the project, such as performance needs, supported compression algorithms, and ease of use.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

lz-string

Node.js CI Version npm package Downloads Documentation

LZ-based compression algorithm for JavaScript

[!IMPORTANT] The file layout has changed in version 2, this is now a joint commonjs / esmodule project so modern build tools should be happy with it, but if importing a file directly (such as in a direct javascript project) it is important to use the correct one.

[!TIP] The "old style" minified AMD file is available as dist/index.umd.js via various CDNs or package managers.

Install via npm

$ npm install -g lz-string
$ lz-string input.txt > output.txt

Home page

Home page for this program with examples, documentation and a live demo: http://pieroxy.net/blog/pages/lz-string/index.html

Command line

If installed globally there is a command line tool available, and a test suite that can use it to show things are working properly. If other langauges build a command line tool that supports the same arguments then the test suite can be run against them too.

$ lz-string -h
Usage: cli [options] [input-file]

Use lz-string to compress or decompress a file

Arguments:
  input-file                  file to process, if no file then read from stdin

Options:
  -V, --version               output the version number
  -d, --decompress            if unset then this will compress
  -e, --encoder <type>        character encoding to use (choices: "base64", "encodeduri", "raw", "uint8array", "utf16", default: "raw")
  -v, --verify                verify before returning (default: true)
  -b, --binary <file>         lz-string binary to use (default: "../dist/index.js")
  -l, --legacy                use legacy mode where uint8array decompression must be an even length
  -o, --output <output-file>  output file, otherwise write to stdout
  -q, --quiet                 don't print any error messages
  -h, --help                  display help for command

Other languages

This lib has numerous ports to other languages, for server side processing, mostly. Here they are:

[!CAUTION] These are all developed separately, so if you are using two versions to transfer data (such as a client and server version) it is important to check that they are compatible and have identical behaviours on the data!

[!NOTE] Version 1.3.8 of this package had a slight change in the encoding which might impact compatibility.

NPM DownloadsLast 30 Days