UNPKG

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