pako
非常快的zlib兼容的JavaScript压缩。
为什么丸子很酷
- Binary-equivalent输出。 Pako产生的deflate/gzip字节与 原始 zlib (1.3.2)和Node.js的补丁zlib。
- 微小的浏览器包。 完整的迷你包压缩在15K以下。 只压缩和只膨胀的构建更小。
- 非常快。 性能与现代JavaScript中的原生zlib相当 引擎(参见基准测试)。
基准
节点v24, 1 MB输入样本:
deflate-pako x 14.27 ops/sec ±3.41% (37 runs sampled)
deflate-pako-zlib-hash x 10.60 ops/sec ±0.50% (29 runs sampled)
deflate-zlib x 30.30 ops/sec ±0.61% (51 runs sampled)
gzip-pako x 13.48 ops/sec ±0.50% (36 runs sampled)
inflate-pako x 138 ops/sec ±1.26% (75 runs sampled)
inflate-zlib x 397 ops/sec ±1.37% (81 runs sampled)
ungzip-pako x 125 ops/sec ±1.46% (73 runs sampled)
安装
npm install pako
[!请注意) 快速浏览一下
dist/文件夹内容,请参见 https://unpkg.com/pako@latest/。
示例/ API
完整文件- http://nodeca.github.io/pako/
import { Deflate, Inflate, deflate, inflate } from 'pako';
// Deflate
//
const input = new Uint8Array();
//... fill input data here
const output = deflate(input);
// Inflate (simple wrapper can throw exception on broken stream)
//
const compressed = new Uint8Array();
//... fill data to uncompress here
try {
const result = inflate(compressed);
// ... continue processing
} catch (err) {
console.log(err);
}
//
// Alternate interface for chunking & without exceptions
//
const deflator = new 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 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;
CommonJS:
const { deflate, inflate } = require('pako');
如果你需要整个API作为一个对象,使用namespace import:
import * as pako from 'pako';
有时您可能希望使用字符串-例如,发送
字符串化对象到服务器。Pako的deflate检测输入数据类型和
在压缩之前自动将字符串重新编码为utf-8。高级膨胀
helper可以将utf-8输出解码回JavaScript字符串 toText: true。
import { deflate, inflate } from 'pako';
const test = { my: 'super', puper: [456, 567], awesome: 'pako' };
const compressed = deflate(JSON.stringify(test));
const restored = JSON.parse(inflate(compressed, { toText: true }));
笔记
Pako不包含一些特定的zlib函数:
- 缩小 - - - - - -方法
deflateCopy,deflateBound,deflateParams,deflatePending,deflatePrime,deflateTune。 - 膨胀 - - - - - -方法
inflateCopy,inflateMark,inflatePrime,inflateGetDictionary,inflateSync,inflateSyncPoint,inflateUndermine。 - 高级充气/放气包装器(类)可能不支持某些刷新 模式。
作者
个人感谢:
- 维亚切斯拉夫·叶戈罗夫(@mraleph)因为他太棒了 关于为v8优化JS代码的教程; IRHydra Tool和他的建议。
- 大卫·杜邦切尔(@dduponchel)寻求帮助 测试。
原始实现(C语言):
- zlib 作者:Jean-loup Gailly和Mark Adler。
许可证
- MIT -所有文件,除了
/src/zlib文件夹 - ZLIB -
/src/zlib内容