# lz-utils

> Utils for string compression and decompression with multiple strategies for Node.js and browser

Latest version **2.1.1** (published 2026-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install lz-utils
pnpm add lz-utils
yarn add lz-utils
bun add lz-utils
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2026-04-11 |
| First published | 2021-12-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 66 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | cenfun |
| Keywords | lz-utils |

## Links

- npm: https://www.npmjs.com/package/lz-utils
- Repository: https://github.com/cenfun/lz-utils
- Homepage: https://github.com/cenfun/lz-utils#readme
- Issues: https://github.com/cenfun/lz-utils/issues
- npm.io page: https://npm.io/package/lz-utils

## Recent versions

- 2.1.1 (latest) — 2026-04-11
- 2.1.0 — 2024-07-25
- 2.0.2 — 2024-01-10
- 2.0.1 — 2023-05-08
- 2.0.0 — 2023-05-08
- 1.0.7 — 2023-05-03
- 1.0.6 — 2023-03-26
- 1.0.5 — 2022-04-20
- 1.0.4 — 2021-12-07
- 1.0.3 — 2021-12-07
- 1.0.2 — 2021-12-07
- 1.0.1 — 2021-12-06
- 1.0.0 — 2021-12-06

## README

# lz-utils - Utils for string compression

[![](https://img.shields.io/npm/v/lz-utils)](https://www.npmjs.com/package/lz-utils)
![](https://img.shields.io/librariesio/github/cenfun/lz-utils)
![](https://img.shields.io/librariesio/dependents/npm/lz-utils)
[![](https://badgen.net/npm/dw/lz-utils)](https://www.npmjs.com/package/lz-utils)
![](https://img.shields.io/github/license/cenfun/lz-utils)

## Features
- Zero runtime dependencies
- Multiple compression strategies for different environments
- ESM and CommonJS support
- TypeScript type definitions included
- Browser bundle (IIFE) included

## API

| Method | Platform | Sync/Async | Algorithm | Minified |
|:---|:---|:---|:---|:---|
| `deflateSync(str)` | Node.js | Sync | Native `zlib` | 0.13 KB |
| `deflate(str)` | Node.js | Async | Native `zlib` | 0.17 KB |
| `inflateSync(str)` | Browser | Sync | [tiny-inflate](https://github.com/foliojs/tiny-inflate) | 3.15 KB |
| `inflate(str)` | Browser | Async (Worker) | [tiny-inflate](https://github.com/foliojs/tiny-inflate) | 3.60 KB |
| `compress(str)` | Both | Sync | [lz-string](https://github.com/pieroxy/lz-string) | 1.71 KB |
| `decompress(str)` | Both | Sync | [lz-string](https://github.com/pieroxy/lz-string) | 1.59 KB |
| `createScriptLoader(str)` | Node.js | Sync | `zlib` + loader template | 4.03 KB |

- `deflateSync` / `deflate` - Compress raw string and encode in base64 (Node.js only, highest performance)
- `inflateSync` / `inflate` - Decompress base64 string to raw string (Browser only, `inflate` uses Web Worker for multi-thread decompression)
- `compress` / `decompress` - LZ-string based compression/decompression in base64 (works on both browser and Node.js)
- `createScriptLoader` - Create a self-contained HTML script that decompresses and executes the given string in browser

## Install
```sh
npm install lz-utils
```

## Usage

### Node.js - Compress with deflate (recommended)
```js
const { deflateSync, deflate } = require('lz-utils');

const raw = "this is string";

// Synchronous
const compressed = deflateSync(raw);

// Asynchronous
const compressed = await deflate(raw);
```

### Browser - Decompress with inflate
```js
import { inflateSync, inflate } from 'lz-utils/inflate-sync';
// or
import { inflate } from 'lz-utils/inflate';

// Synchronous
const raw = inflateSync(compressed);

// Asynchronous (Web Worker)
const raw = await inflate(compressed);
```

### Cross-platform - LZ-string compress/decompress
```js
import { compress, decompress } from 'lz-utils';

const raw = "this is string";
const compressed = compress(raw);
const decompressed = decompress(compressed);
```

### Create script loader
```js
const { createScriptLoader } = require('lz-utils');

// Creates a self-contained HTML script that will decompress and execute in browser
const script = createScriptLoader('console.log("hello")');
```

### Subpath imports
Each function can be imported individually to minimize bundle size:
```js
import compress from 'lz-utils/compress';
import decompress from 'lz-utils/decompress';
import deflate from 'lz-utils/deflate';
import deflateSync from 'lz-utils/deflate-sync';
import inflate from 'lz-utils/inflate';
import inflateSync from 'lz-utils/inflate-sync';
import createScriptLoader from 'lz-utils/create-script-loader';
```

## Examples
- [test.js](/scripts/test.js) - Node.js test
- [test.html](/test/test.html) - Browser test

## Why lz-utils?

lz-utils is designed for a specific workflow: generating self-contained HTML reports.

- **Goal** - Generate HTML reports that are as `fast` to produce as possible and as `small` in file size as possible.
- **How it works** - Report data is compressed on the server (Node.js) and bundled into a single HTML file. When the user opens the file in the browser, the data is `self-decompressed` and rendered on the fly. This is why `deflate` runs on Node.js and `inflate` runs in the browser.
- **Why base64?** - The compressed output is `binary` data, which cannot be safely embedded in JavaScript (due to encoding and CORS issues). Converting it to `base64` produces a plain JS string that can be reliably stored in JSON and embedded in HTML without any escaping problems.

## String Compression Benchmark
- [string-compression](https://github.com/cenfun/string-compression) - lz-string, pako, uzip.js, fflate, tiny-inflate   

## Links
- [pieroxy/lz-string](https://github.com/pieroxy/lz-string) - LZ-based compression algorithm
- [foliojs/tiny-inflate](https://github.com/foliojs/tiny-inflate) - Tiny DEFLATE decompression library

## [Changelog](CHANGELOG.md)

---
_Source: https://npm.io/package/lz-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
