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.

378 lines
16 KiB

4 years ago
  1. # buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][npm-url]
  2. #### The buffer module from [node.js](https://nodejs.org/), for the browser.
  3. [![saucelabs][saucelabs-image]][saucelabs-url]
  4. [travis-image]: https://img.shields.io/travis/feross/buffer/master.svg
  5. [travis-url]: https://travis-ci.org/feross/buffer
  6. [npm-image]: https://img.shields.io/npm/v/buffer.svg
  7. [npm-url]: https://npmjs.org/package/buffer
  8. [downloads-image]: https://img.shields.io/npm/dm/buffer.svg
  9. [saucelabs-image]: https://saucelabs.com/browser-matrix/buffer.svg
  10. [saucelabs-url]: https://saucelabs.com/u/buffer
  11. With [browserify](http://browserify.org), simply `require('buffer')` or use the `Buffer` global and you will get this module.
  12. The goal is to provide an API that is 100% identical to
  13. [node's Buffer API](https://nodejs.org/api/buffer.html). Read the
  14. [official docs](https://nodejs.org/api/buffer.html) for the full list of properties,
  15. instance methods, and class methods that are supported.
  16. ## features
  17. - Manipulate binary data like a boss, in all browsers -- even IE6!
  18. - Super fast. Backed by Typed Arrays (`Uint8Array`/`ArrayBuffer`, not `Object`)
  19. - Extremely small bundle size (**5.04KB minified + gzipped**, 35.5KB with comments)
  20. - Excellent browser support (IE 6+, Chrome 4+, Firefox 3+, Safari 5.1+, Opera 11+, iOS, etc.)
  21. - Preserves Node API exactly, with one minor difference (see below)
  22. - Square-bracket `buf[4]` notation works, even in old browsers like IE6!
  23. - Does not modify any browser prototypes or put anything on `window`
  24. - Comprehensive test suite (including all buffer tests from node.js core)
  25. ## install
  26. To use this module directly (without browserify), install it:
  27. ```bash
  28. npm install buffer
  29. ```
  30. This module was previously called **native-buffer-browserify**, but please use **buffer**
  31. from now on.
  32. A standalone bundle is available [here](https://wzrd.in/standalone/buffer), for non-browserify users.
  33. ## usage
  34. The module's API is identical to node's `Buffer` API. Read the
  35. [official docs](https://nodejs.org/api/buffer.html) for the full list of properties,
  36. instance methods, and class methods that are supported.
  37. As mentioned above, `require('buffer')` or use the `Buffer` global with
  38. [browserify](http://browserify.org) and this module will automatically be included
  39. in your bundle. Almost any npm module will work in the browser, even if it assumes that
  40. the node `Buffer` API will be available.
  41. To depend on this module explicitly (without browserify), require it like this:
  42. ```js
  43. var Buffer = require('buffer/').Buffer // note: the trailing slash is important!
  44. ```
  45. To require this module explicitly, use `require('buffer/')` which tells the node.js module
  46. lookup algorithm (also used by browserify) to use the **npm module** named `buffer`
  47. instead of the **node.js core** module named `buffer`!
  48. ## how does it work?
  49. The Buffer constructor returns instances of `Uint8Array` that have their prototype
  50. changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of `Uint8Array`,
  51. so the returned instances will have all the node `Buffer` methods and the
  52. `Uint8Array` methods. Square bracket notation works as expected -- it returns a
  53. single octet.
  54. The `Uint8Array` prototype remains unmodified.
  55. ## one minor difference
  56. #### In old browsers, `buf.slice()` does not modify parent buffer's memory
  57. If you only support modern browsers (specifically, those with typed array support),
  58. then this issue does not affect you. If you support super old browsers, then read on.
  59. In node, the `slice()` method returns a new `Buffer` that shares underlying memory
  60. with the original Buffer. When you modify one buffer, you modify the other.
  61. [Read more.](https://nodejs.org/api/buffer.html#buffer_buf_slice_start_end)
  62. In browsers with typed array support, this `Buffer` implementation supports this
  63. behavior. In browsers without typed arrays, an alternate buffer implementation is
  64. used that is based on `Object` which has no mechanism to point separate
  65. `Buffer`s to the same underlying slab of memory.
  66. You can see which browser versions lack typed array support
  67. [here](https://github.com/feross/buffer/blob/master/index.js#L22-L48).
  68. ## tracking the latest node api
  69. This module tracks the Buffer API in the latest (unstable) version of node.js. The Buffer
  70. API is considered **stable** in the
  71. [node stability index](https://nodejs.org/docs/latest/api/documentation.html#documentation_stability_index),
  72. so it is unlikely that there will ever be breaking changes.
  73. Nonetheless, when/if the Buffer API changes in node, this module's API will change
  74. accordingly.
  75. ## related packages
  76. - [`buffer-equals`](https://www.npmjs.com/package/buffer-equals) - Node.js 0.12 buffer.equals() ponyfill
  77. - [`buffer-reverse`](https://www.npmjs.com/package/buffer-reverse) - A lite module for reverse-operations on buffers
  78. - [`buffer-xor`](https://www.npmjs.com/package/buffer-xor) - A simple module for bitwise-xor on buffers
  79. - [`is-buffer`](https://www.npmjs.com/package/is-buffer) - Determine if an object is a Buffer without including the whole `Buffer` package
  80. - [`typedarray-to-buffer`](https://www.npmjs.com/package/typedarray-to-buffer) - Convert a typed array to a Buffer without a copy
  81. ## performance
  82. See perf tests in `/perf`.
  83. `BrowserBuffer` is the browser `buffer` module (this repo). `Uint8Array` is included as a
  84. sanity check (since `BrowserBuffer` uses `Uint8Array` under the hood, `Uint8Array` will
  85. always be at least a bit faster). Finally, `NodeBuffer` is the node.js buffer module,
  86. which is included to compare against.
  87. NOTE: Performance has improved since these benchmarks were taken. PR welcoem to update the README.
  88. ### Chrome 38
  89. | Method | Operations | Accuracy | Sampled | Fastest |
  90. |:-------|:-----------|:---------|:--------|:-------:|
  91. | BrowserBuffer#bracket-notation | 11,457,464 ops/sec | ±0.86% | 66 | ✓ |
  92. | Uint8Array#bracket-notation | 10,824,332 ops/sec | ±0.74% | 65 | |
  93. | | | | |
  94. | BrowserBuffer#concat | 450,532 ops/sec | ±0.76% | 68 | |
  95. | Uint8Array#concat | 1,368,911 ops/sec | ±1.50% | 62 | ✓ |
  96. | | | | |
  97. | BrowserBuffer#copy(16000) | 903,001 ops/sec | ±0.96% | 67 | |
  98. | Uint8Array#copy(16000) | 1,422,441 ops/sec | ±1.04% | 66 | ✓ |
  99. | | | | |
  100. | BrowserBuffer#copy(16) | 11,431,358 ops/sec | ±0.46% | 69 | |
  101. | Uint8Array#copy(16) | 13,944,163 ops/sec | ±1.12% | 68 | ✓ |
  102. | | | | |
  103. | BrowserBuffer#new(16000) | 106,329 ops/sec | ±6.70% | 44 | |
  104. | Uint8Array#new(16000) | 131,001 ops/sec | ±2.85% | 31 | ✓ |
  105. | | | | |
  106. | BrowserBuffer#new(16) | 1,554,491 ops/sec | ±1.60% | 65 | |
  107. | Uint8Array#new(16) | 6,623,930 ops/sec | ±1.66% | 65 | ✓ |
  108. | | | | |
  109. | BrowserBuffer#readDoubleBE | 112,830 ops/sec | ±0.51% | 69 | ✓ |
  110. | DataView#getFloat64 | 93,500 ops/sec | ±0.57% | 68 | |
  111. | | | | |
  112. | BrowserBuffer#readFloatBE | 146,678 ops/sec | ±0.95% | 68 | ✓ |
  113. | DataView#getFloat32 | 99,311 ops/sec | ±0.41% | 67 | |
  114. | | | | |
  115. | BrowserBuffer#readUInt32LE | 843,214 ops/sec | ±0.70% | 69 | ✓ |
  116. | DataView#getUint32 | 103,024 ops/sec | ±0.64% | 67 | |
  117. | | | | |
  118. | BrowserBuffer#slice | 1,013,941 ops/sec | ±0.75% | 67 | |
  119. | Uint8Array#subarray | 1,903,928 ops/sec | ±0.53% | 67 | ✓ |
  120. | | | | |
  121. | BrowserBuffer#writeFloatBE | 61,387 ops/sec | ±0.90% | 67 | |
  122. | DataView#setFloat32 | 141,249 ops/sec | ±0.40% | 66 | ✓ |
  123. ### Firefox 33
  124. | Method | Operations | Accuracy | Sampled | Fastest |
  125. |:-------|:-----------|:---------|:--------|:-------:|
  126. | BrowserBuffer#bracket-notation | 20,800,421 ops/sec | ±1.84% | 60 | |
  127. | Uint8Array#bracket-notation | 20,826,235 ops/sec | ±2.02% | 61 | ✓ |
  128. | | | | |
  129. | BrowserBuffer#concat | 153,076 ops/sec | ±2.32% | 61 | |
  130. | Uint8Array#concat | 1,255,674 ops/sec | ±8.65% | 52 | ✓ |
  131. | | | | |
  132. | BrowserBuffer#copy(16000) | 1,105,312 ops/sec | ±1.16% | 63 | |
  133. | Uint8Array#copy(16000) | 1,615,911 ops/sec | ±0.55% | 66 | ✓ |
  134. | | | | |
  135. | BrowserBuffer#copy(16) | 16,357,599 ops/sec | ±0.73% | 68 | |
  136. | Uint8Array#copy(16) | 31,436,281 ops/sec | ±1.05% | 68 | ✓ |
  137. | | | | |
  138. | BrowserBuffer#new(16000) | 52,995 ops/sec | ±6.01% | 35 | |
  139. | Uint8Array#new(16000) | 87,686 ops/sec | ±5.68% | 45 | ✓ |
  140. | | | | |
  141. | BrowserBuffer#new(16) | 252,031 ops/sec | ±1.61% | 66 | |
  142. | Uint8Array#new(16) | 8,477,026 ops/sec | ±0.49% | 68 | ✓ |
  143. | | | | |
  144. | BrowserBuffer#readDoubleBE | 99,871 ops/sec | ±0.41% | 69 | |
  145. | DataView#getFloat64 | 285,663 ops/sec | ±0.70% | 68 | ✓ |
  146. | | | | |
  147. | BrowserBuffer#readFloatBE | 115,540 ops/sec | ±0.42% | 69 | |
  148. | DataView#getFloat32 | 288,722 ops/sec | ±0.82% | 68 | ✓ |
  149. | | | | |
  150. | BrowserBuffer#readUInt32LE | 633,926 ops/sec | ±1.08% | 67 | ✓ |
  151. | DataView#getUint32 | 294,808 ops/sec | ±0.79% | 64 | |
  152. | | | | |
  153. | BrowserBuffer#slice | 349,425 ops/sec | ±0.46% | 69 | |
  154. | Uint8Array#subarray | 5,965,819 ops/sec | ±0.60% | 65 | ✓ |
  155. | | | | |
  156. | BrowserBuffer#writeFloatBE | 59,980 ops/sec | ±0.41% | 67 | |
  157. | DataView#setFloat32 | 317,634 ops/sec | ±0.63% | 68 | ✓ |
  158. ### Safari 8
  159. | Method | Operations | Accuracy | Sampled | Fastest |
  160. |:-------|:-----------|:---------|:--------|:-------:|
  161. | BrowserBuffer#bracket-notation | 10,279,729 ops/sec | ±2.25% | 56 | ✓ |
  162. | Uint8Array#bracket-notation | 10,030,767 ops/sec | ±2.23% | 59 | |
  163. | | | | |
  164. | BrowserBuffer#concat | 144,138 ops/sec | ±1.38% | 65 | |
  165. | Uint8Array#concat | 4,950,764 ops/sec | ±1.70% | 63 | ✓ |
  166. | | | | |
  167. | BrowserBuffer#copy(16000) | 1,058,548 ops/sec | ±1.51% | 64 | |
  168. | Uint8Array#copy(16000) | 1,409,666 ops/sec | ±1.17% | 65 | ✓ |
  169. | | | | |
  170. | BrowserBuffer#copy(16) | 6,282,529 ops/sec | ±1.88% | 58 | |
  171. | Uint8Array#copy(16) | 11,907,128 ops/sec | ±2.87% | 58 | ✓ |
  172. | | | | |
  173. | BrowserBuffer#new(16000) | 101,663 ops/sec | ±3.89% | 57 | |
  174. | Uint8Array#new(16000) | 22,050,818 ops/sec | ±6.51% | 46 | ✓ |
  175. | | | | |
  176. | BrowserBuffer#new(16) | 176,072 ops/sec | ±2.13% | 64 | |
  177. | Uint8Array#new(16) | 24,385,731 ops/sec | ±5.01% | 51 | ✓ |
  178. | | | | |
  179. | BrowserBuffer#readDoubleBE | 41,341 ops/sec | ±1.06% | 67 | |
  180. | DataView#getFloat64 | 322,280 ops/sec | ±0.84% | 68 | ✓ |
  181. | | | | |
  182. | BrowserBuffer#readFloatBE | 46,141 ops/sec | ±1.06% | 65 | |
  183. | DataView#getFloat32 | 337,025 ops/sec | ±0.43% | 69 | ✓ |
  184. | | | | |
  185. | BrowserBuffer#readUInt32LE | 151,551 ops/sec | ±1.02% | 66 | |
  186. | DataView#getUint32 | 308,278 ops/sec | ±0.94% | 67 | ✓ |
  187. | | | | |
  188. | BrowserBuffer#slice | 197,365 ops/sec | ±0.95% | 66 | |
  189. | Uint8Array#subarray | 9,558,024 ops/sec | ±3.08% | 58 | ✓ |
  190. | | | | |
  191. | BrowserBuffer#writeFloatBE | 17,518 ops/sec | ±1.03% | 63 | |
  192. | DataView#setFloat32 | 319,751 ops/sec | ±0.48% | 68 | ✓ |
  193. ### Node 0.11.14
  194. | Method | Operations | Accuracy | Sampled | Fastest |
  195. |:-------|:-----------|:---------|:--------|:-------:|
  196. | BrowserBuffer#bracket-notation | 10,489,828 ops/sec | ±3.25% | 90 | |
  197. | Uint8Array#bracket-notation | 10,534,884 ops/sec | ±0.81% | 92 | ✓ |
  198. | NodeBuffer#bracket-notation | 10,389,910 ops/sec | ±0.97% | 87 | |
  199. | | | | |
  200. | BrowserBuffer#concat | 487,830 ops/sec | ±2.58% | 88 | |
  201. | Uint8Array#concat | 1,814,327 ops/sec | ±1.28% | 88 | ✓ |
  202. | NodeBuffer#concat | 1,636,523 ops/sec | ±1.88% | 73 | |
  203. | | | | |
  204. | BrowserBuffer#copy(16000) | 1,073,665 ops/sec | ±0.77% | 90 | |
  205. | Uint8Array#copy(16000) | 1,348,517 ops/sec | ±0.84% | 89 | ✓ |
  206. | NodeBuffer#copy(16000) | 1,289,533 ops/sec | ±0.82% | 93 | |
  207. | | | | |
  208. | BrowserBuffer#copy(16) | 12,782,706 ops/sec | ±0.74% | 85 | |
  209. | Uint8Array#copy(16) | 14,180,427 ops/sec | ±0.93% | 92 | ✓ |
  210. | NodeBuffer#copy(16) | 11,083,134 ops/sec | ±1.06% | 89 | |
  211. | | | | |
  212. | BrowserBuffer#new(16000) | 141,678 ops/sec | ±3.30% | 67 | |
  213. | Uint8Array#new(16000) | 161,491 ops/sec | ±2.96% | 60 | |
  214. | NodeBuffer#new(16000) | 292,699 ops/sec | ±3.20% | 55 | ✓ |
  215. | | | | |
  216. | BrowserBuffer#new(16) | 1,655,466 ops/sec | ±2.41% | 82 | |
  217. | Uint8Array#new(16) | 14,399,926 ops/sec | ±0.91% | 94 | ✓ |
  218. | NodeBuffer#new(16) | 3,894,696 ops/sec | ±0.88% | 92 | |
  219. | | | | |
  220. | BrowserBuffer#readDoubleBE | 109,582 ops/sec | ±0.75% | 93 | ✓ |
  221. | DataView#getFloat64 | 91,235 ops/sec | ±0.81% | 90 | |
  222. | NodeBuffer#readDoubleBE | 88,593 ops/sec | ±0.96% | 81 | |
  223. | | | | |
  224. | BrowserBuffer#readFloatBE | 139,854 ops/sec | ±1.03% | 85 | ✓ |
  225. | DataView#getFloat32 | 98,744 ops/sec | ±0.80% | 89 | |
  226. | NodeBuffer#readFloatBE | 92,769 ops/sec | ±0.94% | 93 | |
  227. | | | | |
  228. | BrowserBuffer#readUInt32LE | 710,861 ops/sec | ±0.82% | 92 | |
  229. | DataView#getUint32 | 117,893 ops/sec | ±0.84% | 91 | |
  230. | NodeBuffer#readUInt32LE | 851,412 ops/sec | ±0.72% | 93 | ✓ |
  231. | | | | |
  232. | BrowserBuffer#slice | 1,673,877 ops/sec | ±0.73% | 94 | |
  233. | Uint8Array#subarray | 6,919,243 ops/sec | ±0.67% | 90 | ✓ |
  234. | NodeBuffer#slice | 4,617,604 ops/sec | ±0.79% | 93 | |
  235. | | | | |
  236. | BrowserBuffer#writeFloatBE | 66,011 ops/sec | ±0.75% | 93 | |
  237. | DataView#setFloat32 | 127,760 ops/sec | ±0.72% | 93 | ✓ |
  238. | NodeBuffer#writeFloatBE | 103,352 ops/sec | ±0.83% | 93 | |
  239. ### iojs 1.8.1
  240. | Method | Operations | Accuracy | Sampled | Fastest |
  241. |:-------|:-----------|:---------|:--------|:-------:|
  242. | BrowserBuffer#bracket-notation | 10,990,488 ops/sec | ±1.11% | 91 | |
  243. | Uint8Array#bracket-notation | 11,268,757 ops/sec | ±0.65% | 97 | |
  244. | NodeBuffer#bracket-notation | 11,353,260 ops/sec | ±0.83% | 94 | ✓ |
  245. | | | | |
  246. | BrowserBuffer#concat | 378,954 ops/sec | ±0.74% | 94 | |
  247. | Uint8Array#concat | 1,358,288 ops/sec | ±0.97% | 87 | |
  248. | NodeBuffer#concat | 1,934,050 ops/sec | ±1.11% | 78 | ✓ |
  249. | | | | |
  250. | BrowserBuffer#copy(16000) | 894,538 ops/sec | ±0.56% | 84 | |
  251. | Uint8Array#copy(16000) | 1,442,656 ops/sec | ±0.71% | 96 | |
  252. | NodeBuffer#copy(16000) | 1,457,898 ops/sec | ±0.53% | 92 | ✓ |
  253. | | | | |
  254. | BrowserBuffer#copy(16) | 12,870,457 ops/sec | ±0.67% | 95 | |
  255. | Uint8Array#copy(16) | 16,643,989 ops/sec | ±0.61% | 93 | ✓ |
  256. | NodeBuffer#copy(16) | 14,885,848 ops/sec | ±0.74% | 94 | |
  257. | | | | |
  258. | BrowserBuffer#new(16000) | 109,264 ops/sec | ±4.21% | 63 | |
  259. | Uint8Array#new(16000) | 138,916 ops/sec | ±1.87% | 61 | |
  260. | NodeBuffer#new(16000) | 281,449 ops/sec | ±3.58% | 51 | ✓ |
  261. | | | | |
  262. | BrowserBuffer#new(16) | 1,362,935 ops/sec | ±0.56% | 99 | |
  263. | Uint8Array#new(16) | 6,193,090 ops/sec | ±0.64% | 95 | ✓ |
  264. | NodeBuffer#new(16) | 4,745,425 ops/sec | ±1.56% | 90 | |
  265. | | | | |
  266. | BrowserBuffer#readDoubleBE | 118,127 ops/sec | ±0.59% | 93 | ✓ |
  267. | DataView#getFloat64 | 107,332 ops/sec | ±0.65% | 91 | |
  268. | NodeBuffer#readDoubleBE | 116,274 ops/sec | ±0.94% | 95 | |
  269. | | | | |
  270. | BrowserBuffer#readFloatBE | 150,326 ops/sec | ±0.58% | 95 | ✓ |
  271. | DataView#getFloat32 | 110,541 ops/sec | ±0.57% | 98 | |
  272. | NodeBuffer#readFloatBE | 121,599 ops/sec | ±0.60% | 87 | |
  273. | | | | |
  274. | BrowserBuffer#readUInt32LE | 814,147 ops/sec | ±0.62% | 93 | |
  275. | DataView#getUint32 | 137,592 ops/sec | ±0.64% | 90 | |
  276. | NodeBuffer#readUInt32LE | 931,650 ops/sec | ±0.71% | 96 | ✓ |
  277. | | | | |
  278. | BrowserBuffer#slice | 878,590 ops/sec | ±0.68% | 93 | |
  279. | Uint8Array#subarray | 2,843,308 ops/sec | ±1.02% | 90 | |
  280. | NodeBuffer#slice | 4,998,316 ops/sec | ±0.68% | 90 | ✓ |
  281. | | | | |
  282. | BrowserBuffer#writeFloatBE | 65,927 ops/sec | ±0.74% | 93 | |
  283. | DataView#setFloat32 | 139,823 ops/sec | ±0.97% | 89 | ✓ |
  284. | NodeBuffer#writeFloatBE | 135,763 ops/sec | ±0.65% | 96 | |
  285. | | | | |
  286. ## Testing the project
  287. First, install the project:
  288. npm install
  289. Then, to run tests in Node.js, run:
  290. npm run test-node
  291. To test locally in a browser, you can run:
  292. npm run test-browser-local
  293. This will print out a URL that you can then open in a browser to run the tests, using [Zuul](https://github.com/defunctzombie/zuul).
  294. To run automated browser tests using Saucelabs, ensure that your `SAUCE_USERNAME` and `SAUCE_ACCESS_KEY` environment variables are set, then run:
  295. npm test
  296. This is what's run in Travis, to check against various browsers. The list of browsers is kept in the `.zuul.yml` file.
  297. ## JavaScript Standard Style
  298. This module uses [JavaScript Standard Style](https://github.com/feross/standard).
  299. [![JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
  300. To test that the code conforms to the style, `npm install` and run:
  301. ./node_modules/.bin/standard
  302. ## credit
  303. This was originally forked from [buffer-browserify](https://github.com/toots/buffer-browserify).
  304. ## license
  305. MIT. Copyright (C) [Feross Aboukhadijeh](http://feross.org), and other contributors. Originally forked from an MIT-licensed module by Romain Beauxis.