Projekt

Obecné

Profil

Stáhnout (16.2 KB) Statistiky
| Větev: | Revize:
1
# buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][npm-url]
2

    
3
#### The buffer module from [node.js](https://nodejs.org/), for the browser.
4

    
5
[![saucelabs][saucelabs-image]][saucelabs-url]
6

    
7
[travis-image]: https://img.shields.io/travis/feross/buffer/master.svg
8
[travis-url]: https://travis-ci.org/feross/buffer
9
[npm-image]: https://img.shields.io/npm/v/buffer.svg
10
[npm-url]: https://npmjs.org/package/buffer
11
[downloads-image]: https://img.shields.io/npm/dm/buffer.svg
12
[saucelabs-image]: https://saucelabs.com/browser-matrix/buffer.svg
13
[saucelabs-url]: https://saucelabs.com/u/buffer
14

    
15
With [browserify](http://browserify.org), simply `require('buffer')` or use the `Buffer` global and you will get this module.
16

    
17
The goal is to provide an API that is 100% identical to
18
[node's Buffer API](https://nodejs.org/api/buffer.html). Read the
19
[official docs](https://nodejs.org/api/buffer.html) for the full list of properties,
20
instance methods, and class methods that are supported.
21

    
22
## features
23

    
24
- Manipulate binary data like a boss, in all browsers -- even IE6!
25
- Super fast. Backed by Typed Arrays (`Uint8Array`/`ArrayBuffer`, not `Object`)
26
- Extremely small bundle size (**5.04KB minified + gzipped**, 35.5KB with comments)
27
- Excellent browser support (IE 6+, Chrome 4+, Firefox 3+, Safari 5.1+, Opera 11+, iOS, etc.)
28
- Preserves Node API exactly, with one minor difference (see below)
29
- Square-bracket `buf[4]` notation works, even in old browsers like IE6!
30
- Does not modify any browser prototypes or put anything on `window`
31
- Comprehensive test suite (including all buffer tests from node.js core)
32

    
33

    
34
## install
35

    
36
To use this module directly (without browserify), install it:
37

    
38
```bash
39
npm install buffer
40
```
41

    
42
This module was previously called **native-buffer-browserify**, but please use **buffer**
43
from now on.
44

    
45
A standalone bundle is available [here](https://wzrd.in/standalone/buffer), for non-browserify users.
46

    
47

    
48
## usage
49

    
50
The module's API is identical to node's `Buffer` API. Read the
51
[official docs](https://nodejs.org/api/buffer.html) for the full list of properties,
52
instance methods, and class methods that are supported.
53

    
54
As mentioned above, `require('buffer')` or use the `Buffer` global with
55
[browserify](http://browserify.org) and this module will automatically be included
56
in your bundle. Almost any npm module will work in the browser, even if it assumes that
57
the node `Buffer` API will be available.
58

    
59
To depend on this module explicitly (without browserify), require it like this:
60

    
61
```js
62
var Buffer = require('buffer/').Buffer  // note: the trailing slash is important!
63
```
64

    
65
To require this module explicitly, use `require('buffer/')` which tells the node.js module
66
lookup algorithm (also used by browserify) to use the **npm module** named `buffer`
67
instead of the **node.js core** module named `buffer`!
68

    
69

    
70
## how does it work?
71

    
72
The Buffer constructor returns instances of `Uint8Array` that have their prototype
73
changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of `Uint8Array`,
74
so the returned instances will have all the node `Buffer` methods and the
75
`Uint8Array` methods. Square bracket notation works as expected -- it returns a
76
single octet.
77

    
78
The `Uint8Array` prototype remains unmodified.
79

    
80

    
81
## one minor difference
82

    
83
#### In old browsers, `buf.slice()` does not modify parent buffer's memory
84

    
85
If you only support modern browsers (specifically, those with typed array support),
86
then this issue does not affect you. If you support super old browsers, then read on.
87

    
88
In node, the `slice()` method returns a new `Buffer` that shares underlying memory
89
with the original Buffer. When you modify one buffer, you modify the other.
90
[Read more.](https://nodejs.org/api/buffer.html#buffer_buf_slice_start_end)
91

    
92
In browsers with typed array support, this `Buffer` implementation supports this
93
behavior. In browsers without typed arrays, an alternate buffer implementation is
94
used that is based on `Object` which has no mechanism to point separate
95
`Buffer`s to the same underlying slab of memory.
96

    
97
You can see which browser versions lack typed array support
98
[here](https://github.com/feross/buffer/blob/master/index.js#L22-L48).
99

    
100

    
101
## tracking the latest node api
102

    
103
This module tracks the Buffer API in the latest (unstable) version of node.js. The Buffer
104
API is considered **stable** in the
105
[node stability index](https://nodejs.org/docs/latest/api/documentation.html#documentation_stability_index),
106
so it is unlikely that there will ever be breaking changes.
107
Nonetheless, when/if the Buffer API changes in node, this module's API will change
108
accordingly.
109

    
110
## related packages
111

    
112
- [`buffer-equals`](https://www.npmjs.com/package/buffer-equals) - Node.js 0.12 buffer.equals() ponyfill
113
- [`buffer-reverse`](https://www.npmjs.com/package/buffer-reverse) - A lite module for reverse-operations on buffers
114
- [`buffer-xor`](https://www.npmjs.com/package/buffer-xor) - A simple module for bitwise-xor on buffers
115
- [`is-buffer`](https://www.npmjs.com/package/is-buffer) - Determine if an object is a Buffer without including the whole `Buffer` package
116
- [`typedarray-to-buffer`](https://www.npmjs.com/package/typedarray-to-buffer) - Convert a typed array to a Buffer without a copy
117

    
118
## performance
119

    
120
See perf tests in `/perf`.
121

    
122
`BrowserBuffer` is the browser `buffer` module (this repo). `Uint8Array` is included as a
123
sanity check (since `BrowserBuffer` uses `Uint8Array` under the hood, `Uint8Array` will
124
always be at least a bit faster). Finally, `NodeBuffer` is the node.js buffer module,
125
which is included to compare against.
126

    
127
NOTE: Performance has improved since these benchmarks were taken. PR welcoem to update the README.
128

    
129
### Chrome 38
130

    
131
| Method | Operations | Accuracy | Sampled | Fastest |
132
|:-------|:-----------|:---------|:--------|:-------:|
133
| BrowserBuffer#bracket-notation | 11,457,464 ops/sec | ±0.86% | 66 | ✓ |
134
| Uint8Array#bracket-notation | 10,824,332 ops/sec | ±0.74% | 65 | |
135
| | | | |
136
| BrowserBuffer#concat | 450,532 ops/sec | ±0.76% | 68 | |
137
| Uint8Array#concat | 1,368,911 ops/sec | ±1.50% | 62 | ✓ |
138
| | | | |
139
| BrowserBuffer#copy(16000) | 903,001 ops/sec | ±0.96% | 67 | |
140
| Uint8Array#copy(16000) | 1,422,441 ops/sec | ±1.04% | 66 | ✓ |
141
| | | | |
142
| BrowserBuffer#copy(16) | 11,431,358 ops/sec | ±0.46% | 69 | |
143
| Uint8Array#copy(16) | 13,944,163 ops/sec | ±1.12% | 68 | ✓ |
144
| | | | |
145
| BrowserBuffer#new(16000) | 106,329 ops/sec | ±6.70% | 44 | |
146
| Uint8Array#new(16000) | 131,001 ops/sec | ±2.85% | 31 | ✓ |
147
| | | | |
148
| BrowserBuffer#new(16) | 1,554,491 ops/sec | ±1.60% | 65 | |
149
| Uint8Array#new(16) | 6,623,930 ops/sec | ±1.66% | 65 | ✓ |
150
| | | | |
151
| BrowserBuffer#readDoubleBE | 112,830 ops/sec | ±0.51% | 69 | ✓ |
152
| DataView#getFloat64 | 93,500 ops/sec | ±0.57% | 68 | |
153
| | | | |
154
| BrowserBuffer#readFloatBE | 146,678 ops/sec | ±0.95% | 68 | ✓ |
155
| DataView#getFloat32 | 99,311 ops/sec | ±0.41% | 67 | |
156
| | | | |
157
| BrowserBuffer#readUInt32LE | 843,214 ops/sec | ±0.70% | 69 | ✓ |
158
| DataView#getUint32 | 103,024 ops/sec | ±0.64% | 67 | |
159
| | | | |
160
| BrowserBuffer#slice | 1,013,941 ops/sec | ±0.75% | 67 | |
161
| Uint8Array#subarray | 1,903,928 ops/sec | ±0.53% | 67 | ✓ |
162
| | | | |
163
| BrowserBuffer#writeFloatBE | 61,387 ops/sec | ±0.90% | 67 | |
164
| DataView#setFloat32 | 141,249 ops/sec | ±0.40% | 66 | ✓ |
165

    
166

    
167
### Firefox 33
168

    
169
| Method | Operations | Accuracy | Sampled | Fastest |
170
|:-------|:-----------|:---------|:--------|:-------:|
171
| BrowserBuffer#bracket-notation | 20,800,421 ops/sec | ±1.84% | 60 | |
172
| Uint8Array#bracket-notation | 20,826,235 ops/sec | ±2.02% | 61 | ✓ |
173
| | | | |
174
| BrowserBuffer#concat | 153,076 ops/sec | ±2.32% | 61 | |
175
| Uint8Array#concat | 1,255,674 ops/sec | ±8.65% | 52 | ✓ |
176
| | | | |
177
| BrowserBuffer#copy(16000) | 1,105,312 ops/sec | ±1.16% | 63 | |
178
| Uint8Array#copy(16000) | 1,615,911 ops/sec | ±0.55% | 66 | ✓ |
179
| | | | |
180
| BrowserBuffer#copy(16) | 16,357,599 ops/sec | ±0.73% | 68 | |
181
| Uint8Array#copy(16) | 31,436,281 ops/sec | ±1.05% | 68 | ✓ |
182
| | | | |
183
| BrowserBuffer#new(16000) | 52,995 ops/sec | ±6.01% | 35 | |
184
| Uint8Array#new(16000) | 87,686 ops/sec | ±5.68% | 45 | ✓ |
185
| | | | |
186
| BrowserBuffer#new(16) | 252,031 ops/sec | ±1.61% | 66 | |
187
| Uint8Array#new(16) | 8,477,026 ops/sec | ±0.49% | 68 | ✓ |
188
| | | | |
189
| BrowserBuffer#readDoubleBE | 99,871 ops/sec | ±0.41% | 69 | |
190
| DataView#getFloat64 | 285,663 ops/sec | ±0.70% | 68 | ✓ |
191
| | | | |
192
| BrowserBuffer#readFloatBE | 115,540 ops/sec | ±0.42% | 69 | |
193
| DataView#getFloat32 | 288,722 ops/sec | ±0.82% | 68 | ✓ |
194
| | | | |
195
| BrowserBuffer#readUInt32LE | 633,926 ops/sec | ±1.08% | 67 | ✓ |
196
| DataView#getUint32 | 294,808 ops/sec | ±0.79% | 64 | |
197
| | | | |
198
| BrowserBuffer#slice | 349,425 ops/sec | ±0.46% | 69 | |
199
| Uint8Array#subarray | 5,965,819 ops/sec | ±0.60% | 65 | ✓ |
200
| | | | |
201
| BrowserBuffer#writeFloatBE | 59,980 ops/sec | ±0.41% | 67 | |
202
| DataView#setFloat32 | 317,634 ops/sec | ±0.63% | 68 | ✓ |
203

    
204
### Safari 8
205

    
206
| Method | Operations | Accuracy | Sampled | Fastest |
207
|:-------|:-----------|:---------|:--------|:-------:|
208
| BrowserBuffer#bracket-notation | 10,279,729 ops/sec | ±2.25% | 56 | ✓ |
209
| Uint8Array#bracket-notation | 10,030,767 ops/sec | ±2.23% | 59 | |
210
| | | | |
211
| BrowserBuffer#concat | 144,138 ops/sec | ±1.38% | 65 | |
212
| Uint8Array#concat | 4,950,764 ops/sec | ±1.70% | 63 | ✓ |
213
| | | | |
214
| BrowserBuffer#copy(16000) | 1,058,548 ops/sec | ±1.51% | 64 | |
215
| Uint8Array#copy(16000) | 1,409,666 ops/sec | ±1.17% | 65 | ✓ |
216
| | | | |
217
| BrowserBuffer#copy(16) | 6,282,529 ops/sec | ±1.88% | 58 | |
218
| Uint8Array#copy(16) | 11,907,128 ops/sec | ±2.87% | 58 | ✓ |
219
| | | | |
220
| BrowserBuffer#new(16000) | 101,663 ops/sec | ±3.89% | 57 | |
221
| Uint8Array#new(16000) | 22,050,818 ops/sec | ±6.51% | 46 | ✓ |
222
| | | | |
223
| BrowserBuffer#new(16) | 176,072 ops/sec | ±2.13% | 64 | |
224
| Uint8Array#new(16) | 24,385,731 ops/sec | ±5.01% | 51 | ✓ |
225
| | | | |
226
| BrowserBuffer#readDoubleBE | 41,341 ops/sec | ±1.06% | 67 | |
227
| DataView#getFloat64 | 322,280 ops/sec | ±0.84% | 68 | ✓ |
228
| | | | |
229
| BrowserBuffer#readFloatBE | 46,141 ops/sec | ±1.06% | 65 | |
230
| DataView#getFloat32 | 337,025 ops/sec | ±0.43% | 69 | ✓ |
231
| | | | |
232
| BrowserBuffer#readUInt32LE | 151,551 ops/sec | ±1.02% | 66 | |
233
| DataView#getUint32 | 308,278 ops/sec | ±0.94% | 67 | ✓ |
234
| | | | |
235
| BrowserBuffer#slice | 197,365 ops/sec | ±0.95% | 66 | |
236
| Uint8Array#subarray | 9,558,024 ops/sec | ±3.08% | 58 | ✓ |
237
| | | | |
238
| BrowserBuffer#writeFloatBE | 17,518 ops/sec | ±1.03% | 63 | |
239
| DataView#setFloat32 | 319,751 ops/sec | ±0.48% | 68 | ✓ |
240

    
241

    
242
### Node 0.11.14
243

    
244
| Method | Operations | Accuracy | Sampled | Fastest |
245
|:-------|:-----------|:---------|:--------|:-------:|
246
| BrowserBuffer#bracket-notation | 10,489,828 ops/sec | ±3.25% | 90 | |
247
| Uint8Array#bracket-notation | 10,534,884 ops/sec | ±0.81% | 92 | ✓ |
248
| NodeBuffer#bracket-notation | 10,389,910 ops/sec | ±0.97% | 87 | |
249
| | | | |
250
| BrowserBuffer#concat | 487,830 ops/sec | ±2.58% | 88 | |
251
| Uint8Array#concat | 1,814,327 ops/sec | ±1.28% | 88 | ✓ |
252
| NodeBuffer#concat | 1,636,523 ops/sec | ±1.88% | 73 | |
253
| | | | |
254
| BrowserBuffer#copy(16000) | 1,073,665 ops/sec | ±0.77% | 90 | |
255
| Uint8Array#copy(16000) | 1,348,517 ops/sec | ±0.84% | 89 | ✓ |
256
| NodeBuffer#copy(16000) | 1,289,533 ops/sec | ±0.82% | 93 | |
257
| | | | |
258
| BrowserBuffer#copy(16) | 12,782,706 ops/sec | ±0.74% | 85 | |
259
| Uint8Array#copy(16) | 14,180,427 ops/sec | ±0.93% | 92 | ✓ |
260
| NodeBuffer#copy(16) | 11,083,134 ops/sec | ±1.06% | 89 | |
261
| | | | |
262
| BrowserBuffer#new(16000) | 141,678 ops/sec | ±3.30% | 67 | |
263
| Uint8Array#new(16000) | 161,491 ops/sec | ±2.96% | 60 | |
264
| NodeBuffer#new(16000) | 292,699 ops/sec | ±3.20% | 55 | ✓ |
265
| | | | |
266
| BrowserBuffer#new(16) | 1,655,466 ops/sec | ±2.41% | 82 | |
267
| Uint8Array#new(16) | 14,399,926 ops/sec | ±0.91% | 94 | ✓ |
268
| NodeBuffer#new(16) | 3,894,696 ops/sec | ±0.88% | 92 | |
269
| | | | |
270
| BrowserBuffer#readDoubleBE | 109,582 ops/sec | ±0.75% | 93 | ✓ |
271
| DataView#getFloat64 | 91,235 ops/sec | ±0.81% | 90 | |
272
| NodeBuffer#readDoubleBE | 88,593 ops/sec | ±0.96% | 81 | |
273
| | | | |
274
| BrowserBuffer#readFloatBE | 139,854 ops/sec | ±1.03% | 85 | ✓ |
275
| DataView#getFloat32 | 98,744 ops/sec | ±0.80% | 89 | |
276
| NodeBuffer#readFloatBE | 92,769 ops/sec | ±0.94% | 93 | |
277
| | | | |
278
| BrowserBuffer#readUInt32LE | 710,861 ops/sec | ±0.82% | 92 | |
279
| DataView#getUint32 | 117,893 ops/sec | ±0.84% | 91 | |
280
| NodeBuffer#readUInt32LE | 851,412 ops/sec | ±0.72% | 93 | ✓ |
281
| | | | |
282
| BrowserBuffer#slice | 1,673,877 ops/sec | ±0.73% | 94 | |
283
| Uint8Array#subarray | 6,919,243 ops/sec | ±0.67% | 90 | ✓ |
284
| NodeBuffer#slice | 4,617,604 ops/sec | ±0.79% | 93 | |
285
| | | | |
286
| BrowserBuffer#writeFloatBE | 66,011 ops/sec | ±0.75% | 93 | |
287
| DataView#setFloat32 | 127,760 ops/sec | ±0.72% | 93 | ✓ |
288
| NodeBuffer#writeFloatBE | 103,352 ops/sec | ±0.83% | 93 | |
289

    
290
### iojs 1.8.1
291

    
292
| Method | Operations | Accuracy | Sampled | Fastest |
293
|:-------|:-----------|:---------|:--------|:-------:|
294
| BrowserBuffer#bracket-notation | 10,990,488 ops/sec | ±1.11% | 91 | |
295
| Uint8Array#bracket-notation | 11,268,757 ops/sec | ±0.65% | 97 | |
296
| NodeBuffer#bracket-notation | 11,353,260 ops/sec | ±0.83% | 94 | ✓ |
297
| | | | |
298
| BrowserBuffer#concat | 378,954 ops/sec | ±0.74% | 94 | |
299
| Uint8Array#concat | 1,358,288 ops/sec | ±0.97% | 87 | |
300
| NodeBuffer#concat | 1,934,050 ops/sec | ±1.11% | 78 | ✓ |
301
| | | | |
302
| BrowserBuffer#copy(16000) | 894,538 ops/sec | ±0.56% | 84 | |
303
| Uint8Array#copy(16000) | 1,442,656 ops/sec | ±0.71% | 96 | |
304
| NodeBuffer#copy(16000) | 1,457,898 ops/sec | ±0.53% | 92 | ✓ |
305
| | | | |
306
| BrowserBuffer#copy(16) | 12,870,457 ops/sec | ±0.67% | 95 | |
307
| Uint8Array#copy(16) | 16,643,989 ops/sec | ±0.61% | 93 | ✓ |
308
| NodeBuffer#copy(16) | 14,885,848 ops/sec | ±0.74% | 94 | |
309
| | | | |
310
| BrowserBuffer#new(16000) | 109,264 ops/sec | ±4.21% | 63 | |
311
| Uint8Array#new(16000) | 138,916 ops/sec | ±1.87% | 61 | |
312
| NodeBuffer#new(16000) | 281,449 ops/sec | ±3.58% | 51 | ✓ |
313
| | | | |
314
| BrowserBuffer#new(16) | 1,362,935 ops/sec | ±0.56% | 99 | |
315
| Uint8Array#new(16) | 6,193,090 ops/sec | ±0.64% | 95 | ✓ |
316
| NodeBuffer#new(16) | 4,745,425 ops/sec | ±1.56% | 90 | |
317
| | | | |
318
| BrowserBuffer#readDoubleBE | 118,127 ops/sec | ±0.59% | 93 | ✓ |
319
| DataView#getFloat64 | 107,332 ops/sec | ±0.65% | 91 | |
320
| NodeBuffer#readDoubleBE | 116,274 ops/sec | ±0.94% | 95 | |
321
| | | | |
322
| BrowserBuffer#readFloatBE | 150,326 ops/sec | ±0.58% | 95 | ✓ |
323
| DataView#getFloat32 | 110,541 ops/sec | ±0.57% | 98 | |
324
| NodeBuffer#readFloatBE | 121,599 ops/sec | ±0.60% | 87 | |
325
| | | | |
326
| BrowserBuffer#readUInt32LE | 814,147 ops/sec | ±0.62% | 93 | |
327
| DataView#getUint32 | 137,592 ops/sec | ±0.64% | 90 | |
328
| NodeBuffer#readUInt32LE | 931,650 ops/sec | ±0.71% | 96 | ✓ |
329
| | | | |
330
| BrowserBuffer#slice | 878,590 ops/sec | ±0.68% | 93 | |
331
| Uint8Array#subarray | 2,843,308 ops/sec | ±1.02% | 90 | |
332
| NodeBuffer#slice | 4,998,316 ops/sec | ±0.68% | 90 | ✓ |
333
| | | | |
334
| BrowserBuffer#writeFloatBE | 65,927 ops/sec | ±0.74% | 93 | |
335
| DataView#setFloat32 | 139,823 ops/sec | ±0.97% | 89 | ✓ |
336
| NodeBuffer#writeFloatBE | 135,763 ops/sec | ±0.65% | 96 | |
337
| | | | |
338

    
339
## Testing the project
340

    
341
First, install the project:
342

    
343
    npm install
344

    
345
Then, to run tests in Node.js, run:
346

    
347
    npm run test-node
348

    
349
To test locally in a browser, you can run:
350

    
351
    npm run test-browser-local
352

    
353
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).
354

    
355
To run automated browser tests using Saucelabs, ensure that your `SAUCE_USERNAME` and `SAUCE_ACCESS_KEY` environment variables are set, then run:
356

    
357
    npm test
358

    
359
This is what's run in Travis, to check against various browsers. The list of browsers is kept in the `.zuul.yml` file.
360

    
361
## JavaScript Standard Style
362

    
363
This module uses [JavaScript Standard Style](https://github.com/feross/standard).
364

    
365
[![JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
366

    
367
To test that the code conforms to the style, `npm install` and run:
368

    
369
    ./node_modules/.bin/standard
370

    
371
## credit
372

    
373
This was originally forked from [buffer-browserify](https://github.com/toots/buffer-browserify).
374

    
375

    
376
## license
377

    
378
MIT. Copyright (C) [Feross Aboukhadijeh](http://feross.org), and other contributors. Originally forked from an MIT-licensed module by Romain Beauxis.
(4-4/6)