typedarray-to-buffer
Convert a typed array to a Buffer without a copy.
Say you're using the 'buffer' module on npm, or
browserify and you're working with lots of binary data.
Unfortunately, sometimes the browser or someone else's API gives you an ArrayBuffer
or a typed array like Uint8Array
to work with and you need to convert it to a
Buffer
. What do you do?
Of course: new Buffer(uint8array)
But, alas, every time you do new Buffer(uint8array)
the entire array gets copied.
The Buffer
constructor does a copy; this is
defined by the node docs and the 'buffer' module
matches the node API exactly.
So, how can we avoid this expensive copy in
performance critical applications?
Simply use this module, of course!
install
npm install typedarray-to-buffer
usage
To convert a typed array to a Buffer
without a copy, do this:
var toBuffer = require('typedarray-to-buffer')
var arr = new Uint8Array([1, 2, 3])
arr = toBuffer(arr)
arr.toString()
arr.readUInt16BE(0)
how it works
If the browser supports typed arrays, then toBuffer
will augment the Uint8Array you
pass in with the Buffer
methods and return it. See
how does Buffer work? for more about
how augmentation works.
If the browser doesn't support typed arrays, then toBuffer
will create a new Buffer
object, copy the data into it, and return it. There's no simple performance optimization
we can do for old browsers. Oh well.
If this module is used in node, then it will just call new Buffer
. This is just for
the convenience of modules that work in both node and the browser.
license
MIT. Copyright (C) Feross Aboukhadijeh.