Home

Awesome

<div align="center"> <h1><code>lz4-wasm</code> and <code>lz4-wasm-nodejs</code> </h1>

<strong>Extremely fast compression(200MB/s Firefox, 350Mb/s Chrome) and decompression(600MB/s Firefox, 1400Mb/s Chrome) in the browser or nodejs using wasm.</strong>

</div>

Benchmarks

Test it yourself here: https://pseitz.github.io/lz4-wasm/

InputLibCompressionDecrompressionRatio
66k_JSONlz4 wasm292.43MB/s687.37MB/s0.23
66k_JSONlz4 js101.48MB/s27.06MB/s0.24
66k_JSONfflate24.21MB/s117.80MB/s0.17
65k Textlz4 wasm146.10MB/s610.59MB/s0.55
65k Textlz4 js73.55MB/s28.15MB/s0.56
65k Textfflate15.40MB/s63.83MB/s0.41
34k Textlz4 wasm159.57MB/s562.43MB/s0.57
34k Textlz4 js59.77MB/s28.10MB/s0.58
34k Textfflate15.53MB/s55.79MB/s0.41
1k Textlz4 wasm181.25MB/s362.50MB/s0.78
1k Textlz4 js2.18MB/s45.31MB/s0.77
1k Textfflate5.49MB/s3.64MB/s0.53

🚴 Usage

There are two npm packages:

See usage examples in this repo for browser and nodejs

The wasm module exposes two function compress and decompress. Both accept and return UInt8Array. Internally the lz4 block api is used, the length of the original input is prepended in 32-bit little endian. The wasm code is based on lz4_flex


import * as wasm from "lz4-wasm";

// use TextEncoder to get bytes (UInt8Array) from string
var enc = new TextEncoder();
const compressed = wasm.compress(enc.encode("compress this text, compress this text pls. thx. thx. thx. thx. thx"));
const original = wasm.decompress(compressed);

var dec = new TextDecoder("utf-8");
alert(dec.decode(original))

Making New Releases

Release for bundler

Build. This will optimize usage for inside a bundler like webpack.

RUST_LOG=info wasm-pack build --release

Due to a long standing bug in wasm-pack 0.9.1, manually add these files to pkg/package.json.

    "lz4_wasm_bg.wasm.d.ts",
RUST_LOG=info wasm-pack publish

Release for nodejs

set name in Cargo toml to

name = "lz4-wasm-nodejs"

Build for nodejs

RUST_LOG=info wasm-pack build --release -t nodejs
RUST_LOG=info wasm-pack publish