Buffer
The buffer module from node.js, for the browser.
Install / Use
/learn @feross/BufferREADME
buffer

The buffer module from node.js, for the browser.
With browserify, simply require('buffer') or use the Buffer global and you will get this module.
The goal is to provide an API that is 100% identical to node's Buffer API. Read the official docs for the full list of properties, instance methods, and class methods that are supported.
features
- Manipulate binary data like a boss, in all browsers!
- Super fast. Backed by Typed Arrays (
Uint8Array/ArrayBuffer, notObject) - Extremely small bundle size (6.75KB minified + gzipped, 51.9KB with comments)
- Excellent browser support (Chrome, Firefox, Edge, Safari 11+, iOS 11+, Android, etc.)
- Preserves Node API exactly
- Square-bracket
buf[4]notation works! - Does not modify any browser prototypes or put anything on
window - Comprehensive test suite (including all buffer tests from node.js core)
install
To use this module directly (without browserify), install it:
npm install buffer
This module was previously called native-buffer-browserify, but please use buffer from now on.
If you do not use a bundler, you can use the standalone script.
usage
The module's API is identical to node's Buffer API. Read the
official docs for the full list of properties,
instance methods, and class methods that are supported.
As mentioned above, require('buffer') or use the Buffer global with
browserify and this module will automatically be included
in your bundle. Almost any npm module will work in the browser, even if it assumes that
the node Buffer API will be available.
To depend on this module explicitly (without browserify), require it like this:
var Buffer = require('buffer/').Buffer // note: the trailing slash is important!
To require this module explicitly, use require('buffer/') which tells the node.js module
lookup algorithm (also used by browserify) to use the npm module named buffer
instead of the node.js core module named buffer!
how does it work?
The Buffer constructor returns instances of Uint8Array that have their prototype
changed to Buffer.prototype. Furthermore, Buffer is a subclass of Uint8Array,
so the returned instances will have all the node Buffer methods and the
Uint8Array methods. Square bracket notation works as expected -- it returns a
single octet.
The Uint8Array prototype remains unmodified.
tracking the latest node api
This module tracks the Buffer API in the latest (unstable) version of node.js. The Buffer API is considered stable in the node stability index, so it is unlikely that there will ever be breaking changes. Nonetheless, when/if the Buffer API changes in node, this module's API will change accordingly.
related packages
buffer-reverse- Reverse a bufferbuffer-xor- Bitwise xor a bufferis-buffer- Determine if an object is a Buffer without including the wholeBufferpackage
conversion packages
convert typed array to buffer
Use typedarray-to-buffer to convert any kind of typed array to a Buffer. Does not perform a copy, so it's super fast.
convert buffer to typed array
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.
convert blob to buffer
Use blob-to-buffer to convert a Blob to a Buffer.
convert buffer to blob
To convert a Buffer to a Blob, use the Blob constructor:
var blob = new Blob([ buffer ])
Optionally, specify a mimetype:
var blob = new Blob([ buffer ], { type: 'text/html' })
convert arraybuffer to buffer
To convert an ArrayBuffer to a Buffer, use the Buffer.from function. Does not perform a copy, so it's super fast.
var buffer = Buffer.from(arrayBuffer)
convert buffer to arraybuffer
To convert a Buffer to an ArrayBuffer, use the .buffer property (which is present on all Uint8Array objects):
var arrayBuffer = buffer.buffer.slice(
buffer.byteOffset, buffer.byteOffset + buffer.byteLength
)
Alternatively, use the to-arraybuffer module.
performance
See perf tests in /perf.
BrowserBuffer is the browser buffer module (this repo). Uint8Array is included as an
additional check (since BrowserBuffer uses Uint8Array under the hood, Uint8Array will
always be at least a bit faster). Finally, NodeBuffer is the node.js buffer module,
which is included to compare against.
NOTE: Performance has improved since these benchmarks were taken. PR welcome to update the README.
Chrome 38
| Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 11,457,464 ops/sec | ±0.86% | 66 | ✓ | | Uint8Array#bracket-notation | 10,824,332 ops/sec | ±0.74% | 65 | | | | | | | | BrowserBuffer#concat | 450,532 ops/sec | ±0.76% | 68 | | | Uint8Array#concat | 1,368,911 ops/sec | ±1.50% | 62 | ✓ | | | | | | | BrowserBuffer#copy(16000) | 903,001 ops/sec | ±0.96% | 67 | | | Uint8Array#copy(16000) | 1,422,441 ops/sec | ±1.04% | 66 | ✓ | | | | | | | BrowserBuffer#copy(16) | 11,431,358 ops/sec | ±0.46% | 69 | | | Uint8Array#copy(16) | 13,944,163 ops/sec | ±1.12% | 68 | ✓ | | | | | | | BrowserBuffer#new(16000) | 106,329 ops/sec | ±6.70% | 44 | | | Uint8Array#new(16000) | 131,001 ops/sec | ±2.85% | 31 | ✓ | | | | | | | BrowserBuffer#new(16) | 1,554,491 ops/sec | ±1.60% | 65 | | | Uint8Array#new(16) | 6,623,930 ops/sec | ±1.66% | 65 | ✓ | | | | | | | BrowserBuffer#readDoubleBE | 112,830 ops/sec | ±0.51% | 69 | ✓ | | DataView#getFloat64 | 93,500 ops/sec | ±0.57% | 68 | | | | | | | | BrowserBuffer#readFloatBE | 146,678 ops/sec | ±0.95% | 68 | ✓ | | DataView#getFloat32 | 99,311 ops/sec | ±0.41% | 67 | | | | | | | | BrowserBuffer#readUInt32LE | 843,214 ops/sec | ±0.70% | 69 | ✓ | | DataView#getUint32 | 103,024 ops/sec | ±0.64% | 67 | | | | | | | | BrowserBuffer#slice | 1,013,941 ops/sec | ±0.75% | 67 | | | Uint8Array#subarray | 1,903,928 ops/sec | ±0.53% | 67 | ✓ | | | | | | | BrowserBuffer#writeFloatBE | 61,387 ops/sec | ±0.90% | 67 | | | DataView#setFloat32 | 141,249 ops/sec | ±0.40% | 66 | ✓ |
Firefox 33
| Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 20,800,421 ops/sec | ±1.84% | 60 | | | Uint8Array#bracket-notation | 20,826,235 ops/sec | ±2.02% | 61 | ✓ | | | | | | | BrowserBuffer#concat | 153,076 ops/sec | ±2.32% | 61 | | | Uint8Array#concat | 1,255,674 ops/sec | ±8.65% | 52 | ✓ | | | | | | | BrowserBuffer#copy(16000) | 1,105,312 ops/sec | ±1.16% | 63 | | | Uint8Array#copy(16000) | 1,615,911 ops/sec | ±0.55% | 66 | ✓ | | | | | | | BrowserBuffer#copy(16) | 16,357,599 ops/sec | ±0.73% | 68 | | | Uint8Array#copy(16) | 31,436,281 ops/sec | ±1.05% | 68 | ✓ | | | | | | | BrowserBuffer#new(16000) | 52,995 ops/sec | ±6.01% | 35 | | | Uint8Array#new(16000) | 87,686 ops/sec | ±5.68% | 45 | ✓ | | | | | | | BrowserBuffer#new(16) | 252,031 ops/sec | ±1.61% | 66 | | | Uint8Array#new(16) | 8,477,026 ops/sec | ±0.49% | 68 | ✓ | | | | | | | BrowserBuffer#readDoubleBE | 99,871 ops/sec | ±0.41% | 69 | | | DataView#getFloat64 | 285,663 ops/sec | ±0.70% | 68 | ✓ | | | | | | | BrowserBuffer#readFloatBE | 115,540 ops/sec | ±0.42% | 69 | | | DataView#getFloat32 | 288,722 ops/sec | ±0.82% | 68 | ✓ | | | | | | | BrowserBuffer#readUInt32LE | 633,926 ops/sec | ±1.08% | 67 | ✓ | | DataView#getUint32 | 294,808 ops/sec | ±0.79% | 64 | | | | | | | | BrowserBuffer#slice | 349,425 ops/sec | ±0.46% | 69 | | | Uint8Array#subarray | 5,965,819 ops/sec | ±0.60% | 65 | ✓ | | | | | | | BrowserBuffer#writeFloatBE | 59,980 ops/sec | ±0.41% | 67 | | | DataView#setFloat32 | 317,634 ops/sec | ±0.63% | 68 | ✓ |
Safari 8
| Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 10,279,729 ops/sec | ±2.25% | 56 | ✓ | | Uint8Array#bracket-notation | 10,030,767 ops/sec | ±2.23% | 59 | | | | | | | | BrowserBuffer#concat | 144,138 ops/sec | ±1.38% | 65 | | | Uint8Array#concat | 4,950,764 ops/sec | ±1.70% | 63 | ✓ | | | | | | | BrowserBuffer#copy(16000) | 1,058,548 ops/sec | ±1.51% | 64 | | | Uint8Array#copy(16000) | 1,409,666 ops/sec | ±1.17% | 65 | ✓ | | | | | | | BrowserBuffer#copy(16) | 6,282,529 ops/sec | ±1.88% | 58 | | | Uint8Array#copy(16) | 11,907,128 ops/sec | ±2.87% | 58 | ✓ | | | | | | | BrowserBuffer#new(16000) | 101,663 ops/sec | ±3.89% | 57 | | | Uint8Array#new(16000) | 22,050,818 ops/sec
