Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork801
high speed zlib port to javascript, works in browser & node.js
License
nodeca/pako
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
zlib port to javascript, very fast!
Why pako is cool:
- Results are binary equal to well knownzlib (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 todevelop 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 pakoFull docs -http://nodeca.github.io/pako/
constpako=require('pako');// Deflate//constinput=newUint8Array();//... fill input data hereconstoutput=pako.deflate(input);// Inflate (simple wrapper can throw exception on broken stream)//constcompressed=newUint8Array();//... fill data to uncompress heretry{constresult=pako.inflate(compressed);// ... continue processing}catch(err){console.log(err);}//// Alternate interface for chunking & without exceptions//constdeflator=newpako.Deflate();deflator.push(chunk1,false);deflator.push(chunk2);// second param is false by default....deflator.push(chunk_last,true);// `true` says this chunk is lastif(deflator.err){console.log(deflator.msg);}constoutput=deflator.result;constinflator=newpako.Inflate();inflator.push(chunk1);inflator.push(chunk2);...inflator.push(chunk_last);// no second param because end is auto-detectedif(inflator.err){console.log(inflator.msg);}constoutput=inflator.result;
Sometime you can wish to work with strings. For example, to sendstringified objects to server. Pako's deflate detects input data type, andautomatically recode strings to utf-8 prior to compress. Inflate has specialoption, to say compressed data has utf-8 encoding and should be recoded tojavascript's utf-16.
constpako=require('pako');consttest={my:'super',puper:[456,567],awesome:'pako'};constcompressed=pako.deflate(JSON.stringify(test));constrestored=JSON.parse(pako.inflate(compressed,{to:'string'}));
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 flushmodes.
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.
Personal thanks to:
- Vyacheslav Egorov (@mraleph) for his awesometutorials about optimising JS code for v8,IRHydratool and his advices.
- David Duponchel (@dduponchel) for help withtesting.
Original implementation (in C):
- zlib by Jean-loup Gailly and Mark Adler.
- MIT - all files, except
/lib/zlibfolder - ZLIB -
/lib/zlibcontent
About
high speed zlib port to javascript, works in browser & node.js
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.