• Stars
    star
    5,328
  • Rank 7,430 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

high speed zlib port to javascript, works in browser & node.js

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

More Repositories

1

js-yaml

JavaScript YAML parser and dumper. Very fast.
JavaScript
6,185
star
2

pica

Resize image in browser with high quality and high speed
JavaScript
3,612
star
3

probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
JavaScript
957
star
4

argparse

CLI arguments parser for node.js. JS port of python's argparse module.
JavaScript
484
star
5

babelfish

human friendly i18n for javascript (node.js + browser)
JavaScript
250
star
6

image-blob-reduce

Resize image blobs with high quality. Pica's wrapper to work with file inputs.
JavaScript
245
star
7

tabex

Cross-tab message bus for browsers.
JavaScript
220
star
8

url-unshort

Short links expander for node.js
JavaScript
112
star
9

ndoc

js port of pdoc, with extentions
JavaScript
100
star
10

charlatan

Fake identities generator for node.js (names, addresses, phones, IPs and others). Supports multiple languages.
JavaScript
95
star
11

nodeca

Forums / Blogs / Groups / Classfields / ... platform. Fork this to make your own config.
JavaScript
94
star
12

bag.js

JS / CSS / files loader + key/value storage
JavaScript
88
star
13

idoit

Redis-backed task queue engine with advanced task control and eventual consistency
JavaScript
72
star
14

glur

Fast gaussian blur in pure JavaScript via IIR filer
JavaScript
68
star
15

multimath

WebAssembly wrapper to simplify fast math coding
JavaScript
66
star
16

embedza

Create HTML snippets/embeds from URLs using info from oEmbed, Open Graph, meta tags.
JavaScript
62
star
17

promise-memoize

Memoize promise-returning functions. Includes cache expire and prefetch.
JavaScript
58
star
18

navit

Simple client testing from your scripts
JavaScript
47
star
19

unhomoglyph

Replace all homoglyphs with base characters. Useful to detect similar strings.
JavaScript
38
star
20

nntp-server

NNTP server for readers
JavaScript
26
star
21

puncher

Library to set timestamps in your application requests & genegate profiling tree.
JavaScript
13
star
22

eslint-plugin-nodeca

Indentation check rule for ESLint
JavaScript
12
star
23

nodeca.core

Nodeca core app (admin panel, loader, bundler)
JavaScript
11
star
24

types

Collection of extra types (structures, classes) for JavaScript.
JavaScript
10
star
25

nodeca.users

Nodeca user app (login / register / albums / profiles)
JavaScript
10
star
26

event-wire

Mediator with dynamic responsibility chains
JavaScript
9
star
27

hike-js

Javascript port of Hike (Ruby) - a library for finding files in a set of paths
JavaScript
9
star
28

plurals-cldr

Plurals suport for JS, autogenerated from CLDR.
JavaScript
8
star
29

relimit

Rate limiter with tuneable scheduler and distributed run support
JavaScript
8
star
30

js-yaml-js-types

Extra js types for js-yaml
JavaScript
7
star
31

mimoza

Simple mime-type tools library
JavaScript
7
star
32

pointer

router for node.js / client
JavaScript
6
star
33

nodeca.forum

Nodeca forum app
JavaScript
3
star
34

redis-if

JavaScript
3
star
35

nodeca-design

Nodeca interface mockups
HTML
3
star
36

fontomas

2
star
37

nodeca.blogs

Blogs component for Nodeca
JavaScript
2
star
38

nodeca.clubs

Clubs component for Nodeca
JavaScript
2
star
39

nodeca.market

Market component for Nodeca
JavaScript
1
star
40

charcount

Count visual length of javascript string
JavaScript
1
star