You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.4 KiB

4 years ago
  1. pako
  2. ==========================================
  3. [![Build Status](https://travis-ci.org/nodeca/pako.svg?branch=master)](https://travis-ci.org/nodeca/pako)
  4. [![NPM version](https://img.shields.io/npm/v/pako.svg)](https://www.npmjs.org/package/pako)
  5. > zlib port to javascript, very fast!
  6. __Why pako is cool:__
  7. - Almost as fast in modern JS engines as C implementation (see benchmarks).
  8. - Works in browsers, you can browserify any separate component.
  9. - Chunking support for big blobs.
  10. - Results are binary equal to well known [zlib](http://www.zlib.net/) (now contains ported zlib v1.2.8).
  11. This project was done to understand how fast JS can be and is it necessary to
  12. develop native C modules for CPU-intensive tasks. Enjoy the result!
  13. __Famous projects, using pako:__
  14. - [browserify](http://browserify.org/) (via [browserify-zlib](https://github.com/devongovett/browserify-zlib))
  15. - [JSZip](http://stuk.github.io/jszip/)
  16. - [mincer](https://github.com/nodeca/mincer)
  17. - [JS-Git](https://github.com/creationix/js-git) and
  18. [Tedit](https://chrome.google.com/webstore/detail/tedit-development-environ/ooekdijbnbbjdfjocaiflnjgoohnblgf)
  19. by [@creationix](https://github.com/creationix)
  20. __Benchmarks:__
  21. ```
  22. node v0.10.26, 1mb sample:
  23. deflate-dankogai x 4.73 ops/sec ±0.82% (15 runs sampled)
  24. deflate-gildas x 4.58 ops/sec ±2.33% (15 runs sampled)
  25. deflate-imaya x 3.22 ops/sec ±3.95% (12 runs sampled)
  26. ! deflate-pako x 6.99 ops/sec ±0.51% (21 runs sampled)
  27. deflate-pako-string x 5.89 ops/sec ±0.77% (18 runs sampled)
  28. deflate-pako-untyped x 4.39 ops/sec ±1.58% (14 runs sampled)
  29. * deflate-zlib x 14.71 ops/sec ±4.23% (59 runs sampled)
  30. inflate-dankogai x 32.16 ops/sec ±0.13% (56 runs sampled)
  31. inflate-imaya x 30.35 ops/sec ±0.92% (53 runs sampled)
  32. ! inflate-pako x 69.89 ops/sec ±1.46% (71 runs sampled)
  33. inflate-pako-string x 19.22 ops/sec ±1.86% (49 runs sampled)
  34. inflate-pako-untyped x 17.19 ops/sec ±0.85% (32 runs sampled)
  35. * inflate-zlib x 70.03 ops/sec ±1.64% (81 runs sampled)
  36. node v0.11.12, 1mb sample:
  37. deflate-dankogai x 5.60 ops/sec ±0.49% (17 runs sampled)
  38. deflate-gildas x 5.06 ops/sec ±6.00% (16 runs sampled)
  39. deflate-imaya x 3.52 ops/sec ±3.71% (13 runs sampled)
  40. ! deflate-pako x 11.52 ops/sec ±0.22% (32 runs sampled)
  41. deflate-pako-string x 9.53 ops/sec ±1.12% (27 runs sampled)
  42. deflate-pako-untyped x 5.44 ops/sec ±0.72% (17 runs sampled)
  43. * deflate-zlib x 14.05 ops/sec ±3.34% (63 runs sampled)
  44. inflate-dankogai x 42.19 ops/sec ±0.09% (56 runs sampled)
  45. inflate-imaya x 79.68 ops/sec ±1.07% (68 runs sampled)
  46. ! inflate-pako x 97.52 ops/sec ±0.83% (80 runs sampled)
  47. inflate-pako-string x 45.19 ops/sec ±1.69% (57 runs sampled)
  48. inflate-pako-untyped x 24.35 ops/sec ±2.59% (40 runs sampled)
  49. * inflate-zlib x 60.32 ops/sec ±1.36% (69 runs sampled)
  50. ```
  51. zlib's test is partially affected by marshalling (that make sense for inflate only).
  52. You can change deflate level to 0 in benchmark source, to investigate details.
  53. For deflate level 6 results can be considered as correct.
  54. __Install:__
  55. node.js:
  56. ```
  57. npm install pako
  58. ```
  59. browser:
  60. ```
  61. bower install pako
  62. ```
  63. Example & API
  64. -------------
  65. Full docs - http://nodeca.github.io/pako/
  66. ```javascript
  67. var pako = require('pako');
  68. // Deflate
  69. //
  70. var input = new Uint8Array();
  71. //... fill input data here
  72. var output = pako.deflate(input);
  73. // Inflate (simple wrapper can throw exception on broken stream)
  74. //
  75. var compressed = new Uint8Array();
  76. //... fill data to uncompress here
  77. try {
  78. var result = pako.inflate(compressed);
  79. } catch (err) {
  80. console.log(err);
  81. }
  82. //
  83. // Alternate interface for chunking & without exceptions
  84. //
  85. var inflator = new pako.Inflate();
  86. inflator.push(chunk1, false);
  87. inflator.push(chunk2, false);
  88. ...
  89. inflator.push(chunkN, true); // true -> last chunk
  90. if (inflator.err) {
  91. console.log(inflator.msg);
  92. }
  93. var output = inflator.result;
  94. ```
  95. Sometime you can wish to work with strings. For example, to send
  96. big objects as json to server. Pako detects input data type. You can
  97. force output to be string with option `{ to: 'string' }`.
  98. ```javascript
  99. var pako = require('pako');
  100. var test = { my: 'super', puper: [456, 567], awesome: 'pako' };
  101. var binaryString = pako.deflate(JSON.stringify(test), { to: 'string' });
  102. //
  103. // Here you can do base64 encode, make xhr requests and so on.
  104. //
  105. var restored = JSON.parse(pako.inflate(binaryString, { to: 'string' }));
  106. ```
  107. Notes
  108. -----
  109. Pako does not contain some specific zlib functions:
  110. - __deflate__ - methods `deflateCopy`, `deflateBound`, `deflateParams`,
  111. `deflatePending`, `deflatePrime`, `deflateTune`.
  112. - __inflate__ - methods `inflateCopy`, `inflateMark`,
  113. `inflatePrime`, `inflateGetDictionary`, `inflateSync`, `inflateSyncPoint`, `inflateUndermine`.
  114. - High level inflate/deflate wrappers (classes) may not support some flush
  115. modes. Those should work: Z_NO_FLUSH, Z_FINISH, Z_SYNC_FLUSH.
  116. Authors
  117. -------
  118. - Andrey Tupitsin [@anrd83](https://github.com/andr83)
  119. - Vitaly Puzrin [@puzrin](https://github.com/puzrin)
  120. Personal thanks to:
  121. - Vyacheslav Egorov ([@mraleph](https://github.com/mraleph)) for his awesome
  122. tutorials about optimising JS code for v8, [IRHydra](http://mrale.ph/irhydra/)
  123. tool and his advices.
  124. - David Duponchel ([@dduponchel](https://github.com/dduponchel)) for help with
  125. testing.
  126. Original implementation (in C):
  127. - [zlib](http://zlib.net/) by Jean-loup Gailly and Mark Adler.
  128. License
  129. -------
  130. - MIT - all files, except `/lib/zlib` folder
  131. - ZLIB - `/lib/zlib` content