What is pako?
The pako npm package is a high-speed zlib port to JavaScript which works in the browser and node.js. It provides compression and decompression functionalities using the zlib library, which is widely used for data compression.
What are pako's main functionalities?
Compression
This feature allows you to compress a string or binary data using pako's deflate method.
const pako = require('pako');
const input = 'String to compress';
const compressed = pako.deflate(input);
Decompression
This feature allows you to decompress data that was compressed using pako's deflate method or compatible zlib compression.
const pako = require('pako');
const compressed = new Uint8Array([]); // Use a previously compressed Uint8Array
const decompressed = pako.inflate(compressed);
Gzip Compression
This feature allows you to compress data using gzip, which is a file format and a software application used for file compression and decompression.
const pako = require('pako');
const input = 'String to compress';
const compressed = pako.gzip(input);
Gzip Decompression
This feature allows you to decompress data that was compressed using pako's gzip method.
const pako = require('pako');
const compressed = new Uint8Array([]); // Use a previously gzip compressed Uint8Array
const decompressed = pako.ungzip(compressed);
Other packages similar to pako
zlib
The zlib package is a core module in Node.js for compression/decompression. It is similar to pako but is built into Node.js and does not work in the browser without additional bundling or shimming.
jszip
JSZip is a library for creating, reading, and editing .zip files with JavaScript, with a lovely and simple API. While pako focuses on zlib compression, JSZip provides additional functionalities to handle zip files.
compressjs
Compressjs is a pure JavaScript implementation of various data compression algorithms, such as Huffman coding and Burrows-Wheeler transform. It offers a wider range of algorithms than pako, but it might not be as optimized for speed.
fflate
fflate is a high-performance, low-level deflate/inflate compression library that is faster than pako on most benchmarks. It is a newer library that focuses on performance and efficiency.
pako - zlib port to javascript, very fast!
Why pako is cool:
- Almost as fast in modern JS engines as C implementation (see benchmarks).
- Works in browsers, you can browserify any separate component.
- Both Sync & streamable (for big blobs) interfaces.
- It's fresh - ports the latest zlib version (now 1.2.8), results are binary equal.
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 v0.10.26, 1mb sample:
deflate-dankogai x 4.74 ops/sec ±0.68% (15 runs sampled)
deflate-gildas x 4.61 ops/sec ±1.73% (15 runs sampled)
deflate-imaya x 3.10 ops/sec ±3.73% (11 runs sampled)
! deflate-pako x 7.11 ops/sec ±0.26% (21 runs sampled)
deflate-pako-untyped x 4.34 ops/sec ±1.35% (14 runs sampled)
deflate-zlib x 14.34 ops/sec ±2.90% (68 runs sampled)
inflate-dankogai x 31.29 ops/sec ±0.72% (56 runs sampled)
inflate-imaya x 30.49 ops/sec ±0.84% (53 runs sampled)
! inflate-pako x 70.00 ops/sec ±1.60% (71 runs sampled)
inflate-pako-untyped x 17.67 ops/sec ±1.27% (33 runs sampled)
inflate-zlib x 70.82 ops/sec ±1.69% (81 runs sampled)
node v0.11.11, 1mb sample:
deflate-dankogai x 5.61 ops/sec ±0.30% (17 runs sampled)
deflate-gildas x 4.97 ops/sec ±5.68% (16 runs sampled)
deflate-imaya x 3.53 ops/sec ±4.19% (12 runs sampled)
! deflate-pako x 11.52 ops/sec ±0.23% (32 runs sampled)
deflate-pako-untyped x 5.12 ops/sec ±1.44% (17 runs sampled)
deflate-zlib x 14.33 ops/sec ±3.34% (63 runs sampled)
inflate-dankogai x 42.96 ops/sec ±0.19% (57 runs sampled)
inflate-imaya x 85.05 ops/sec ±1.07% (71 runs sampled)
! inflate-pako x 97.58 ops/sec ±0.69% (80 runs sampled)
inflate-pako-untyped x 18.06 ops/sec ±0.65% (56 runs sampled)
inflate-zlib x 60.60 ops/sec ±2.04% (67 runs sampled)
zlib's test is partialy afferted by marshling (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:
node.js:
npm install pako
browser:
bower install pako
Example & API
Full docs - http://nodeca.github.io/pako/
var pako = require('pako');
var input = new Uint8Array();
var output = pako.deflate(input);
var compressed = new Uint8Array();
try {
var result = pako.inflate(compressed);
catch (err) {
console.log(err);
}
var inflator = new pako.Inflate();
inflator.push(chunk1, false);
inflator.push(chunk2, false);
...
inflator.push(chunkN, true);
if (inflator.err) {
console.log(inflator.msg);
}
var output = inflator.result;
Notes
Pako does not contain some specific zlib functions:
- deflate - writing custom gzip headers and methods
deflateSetDictionary
,
deflateParams
, deflateSetHeader
, deflateBound
, deflatePending
. - inflate - getting custom gzip headers and methods
inflateGetDictionary
,
inflateGetHeader
, inflateSetDictionary
, inflateSync
, inflateSyncPoint
,
inflateCopy
, inflateUndermine
, inflateMark
.
Authors
Personal thanks to Vyacheslav Egorov (@mraleph)
for his awesome tutoruals about optimising JS code for v8,
IRHydra tool and his advices.
License
MIT