pako vs node-gzip vs zlib
Node.js 压缩库
pakonode-gzipzlib类似的npm包:

Node.js 压缩库

在 Node.js 开发中,压缩库用于处理数据的压缩和解压缩,以提高数据传输效率和存储效率。这些库提供了不同的功能和性能特征,适用于各种场景,如网络传输、文件存储等。选择合适的压缩库可以显著影响应用的性能和开发体验。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
pako69,479,2486,0591.64 MB273 年前(MIT AND Zlib)
node-gzip055-18 年前MIT
zlib063-1115 年前-

功能对比: pako vs node-gzip vs zlib

性能

  • pako:

    pako 是一个高性能的压缩库,特别是在处理大数据时表现出色。它的压缩和解压缩速度非常快,适合需要高效处理的应用。

  • node-gzip:

    node-gzip 提供了简单的 gzip 压缩和解压缩功能,性能适中,适合小型项目和简单的流处理。

  • zlib:

    zlib 是一个经过优化的库,提供多种压缩算法,性能极佳,适合处理大规模数据和高并发场景。

易用性

  • pako:

    pako 的 API 设计灵活,支持多种压缩格式,适合需要跨平台使用的开发者。

  • node-gzip:

    node-gzip 提供了基于 Promise 的 API,使用简单直观,适合快速上手。

  • zlib:

    zlib 的 API 相对复杂,需要一定的学习成本,但提供了更强大的功能和灵活性,适合高级用户。

兼容性

  • pako:

    pako 兼容性强,支持在浏览器和 Node.js 中使用,适合需要跨平台的项目。

  • node-gzip:

    node-gzip 主要用于 Node.js 环境,兼容性良好,但不适合浏览器环境。

  • zlib:

    zlib 是 Node.js 的内置模块,专为后端开发设计,兼容性强,但不适用于浏览器。

功能

  • pako:

    pako 支持 gzip 和 deflate 格式,功能全面,适合需要多种压缩格式的应用。

  • node-gzip:

    node-gzip 专注于 gzip 格式的压缩,功能相对单一,适合简单的应用场景。

  • zlib:

    zlib 提供多种压缩算法和选项,功能强大,适合复杂的应用需求。

社区支持

  • pako:

    pako 拥有活跃的社区支持,更新频繁,文档齐全,适合开发者使用。

  • node-gzip:

    node-gzip 的社区相对较小,更新频率较低,但足够满足基本需求。

  • zlib:

    zlib 是一个成熟的库,拥有广泛的社区支持和丰富的文档,适合需要长期维护的项目。

如何选择: pako vs node-gzip vs zlib

  • pako:

    选择 pako 如果你需要一个高性能的压缩库,支持 gzip 和 deflate 格式,且希望在浏览器和 Node.js 环境中都能使用。它非常适合需要快速压缩和解压缩的应用,尤其是在前端应用中。

  • node-gzip:

    选择 node-gzip 如果你需要一个简单的、基于 Promise 的 API 来处理 gzip 压缩和解压缩,特别是在处理流数据时。它适合于需要轻量级和易用性的场景。

  • zlib:

    选择 zlib 如果你需要一个功能全面且性能优化的库,能够处理多种压缩格式,并且希望利用 Node.js 的内置模块。它适合于需要复杂压缩选项和高效性能的后端服务。

pako的README

pako

CI NPM version

zlib port to javascript, very fast!

Why pako is cool:

  • Results are binary equal to well known zlib (now contains ported zlib v1.2.8).
  • Almost as fast in modern JS engines as C implementation (see benchmarks).
  • Works in browsers, you can browserify any separate component.

This project was done to understand how fast JS can be and is it necessary to develop native C modules for CPU-intensive tasks. Enjoy the result!

Benchmarks:

node v12.16.3 (zlib 1.2.9), 1mb input sample:

deflate-imaya x 4.75 ops/sec ±4.93% (15 runs sampled)
deflate-pako x 10.38 ops/sec ±0.37% (29 runs sampled)
deflate-zlib x 17.74 ops/sec ±0.77% (46 runs sampled)
gzip-pako x 8.86 ops/sec ±1.41% (29 runs sampled)
inflate-imaya x 107 ops/sec ±0.69% (77 runs sampled)
inflate-pako x 131 ops/sec ±1.74% (82 runs sampled)
inflate-zlib x 258 ops/sec ±0.66% (88 runs sampled)
ungzip-pako x 115 ops/sec ±1.92% (80 runs sampled)

node v14.15.0 (google's zlib), 1mb output sample:

deflate-imaya x 4.93 ops/sec ±3.09% (16 runs sampled)
deflate-pako x 10.22 ops/sec ±0.33% (29 runs sampled)
deflate-zlib x 18.48 ops/sec ±0.24% (48 runs sampled)
gzip-pako x 10.16 ops/sec ±0.25% (28 runs sampled)
inflate-imaya x 110 ops/sec ±0.41% (77 runs sampled)
inflate-pako x 134 ops/sec ±0.66% (83 runs sampled)
inflate-zlib x 402 ops/sec ±0.74% (87 runs sampled)
ungzip-pako x 113 ops/sec ±0.62% (80 runs sampled)

zlib's test is partially affected by marshalling (that make sense for inflate only). You can change deflate level to 0 in benchmark source, to investigate details. For deflate level 6 results can be considered as correct.

Install:

npm install pako

Examples / API

Full docs - http://nodeca.github.io/pako/

const pako = require('pako');

// Deflate
//
const input = new Uint8Array();
//... fill input data here
const output = pako.deflate(input);

// Inflate (simple wrapper can throw exception on broken stream)
//
const compressed = new Uint8Array();
//... fill data to uncompress here
try {
  const result = pako.inflate(compressed);
  // ... continue processing
} catch (err) {
  console.log(err);
}

//
// Alternate interface for chunking & without exceptions
//

const deflator = new pako.Deflate();

deflator.push(chunk1, false);
deflator.push(chunk2); // second param is false by default.
...
deflator.push(chunk_last, true); // `true` says this chunk is last

if (deflator.err) {
  console.log(deflator.msg);
}

const output = deflator.result;


const inflator = new pako.Inflate();

inflator.push(chunk1);
inflator.push(chunk2);
...
inflator.push(chunk_last); // no second param because end is auto-detected

if (inflator.err) {
  console.log(inflator.msg);
}

const output = inflator.result;

Sometime you can wish to work with strings. For example, to send stringified objects to server. Pako's deflate detects input data type, and automatically recode strings to utf-8 prior to compress. Inflate has special option, to say compressed data has utf-8 encoding and should be recoded to javascript's utf-16.

const pako = require('pako');

const test = { my: 'super', puper: [456, 567], awesome: 'pako' };

const compressed = pako.deflate(JSON.stringify(test));

const restored = JSON.parse(pako.inflate(compressed, { to: 'string' }));

Notes

Pako does not contain some specific zlib functions:

  • deflate - methods deflateCopy, deflateBound, deflateParams, deflatePending, deflatePrime, deflateTune.
  • inflate - methods inflateCopy, inflateMark, inflatePrime, inflateGetDictionary, inflateSync, inflateSyncPoint, inflateUndermine.
  • High level inflate/deflate wrappers (classes) may not support some flush modes.

pako for enterprise

Available as part of the Tidelift Subscription

The maintainers of pako and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Authors

Personal thanks to:

  • Vyacheslav Egorov (@mraleph) for his awesome tutorials about optimising JS code for v8, IRHydra tool and his advices.
  • David Duponchel (@dduponchel) for help with testing.

Original implementation (in C):

  • zlib by Jean-loup Gailly and Mark Adler.

License

  • MIT - all files, except /lib/zlib folder
  • ZLIB - /lib/zlib content