All Projects → feross → typedarray-to-buffer

feross / typedarray-to-buffer

Licence: MIT license
Convert a typed array to a Buffer without a copy.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to typedarray-to-buffer

Buffer
The buffer module from node.js, for the browser.
Stars: ✭ 1,178 (+1740.63%)
Mutual labels:  browserify, buffer
react-tutorial
A react-tutorial
Stars: ✭ 99 (+54.69%)
Mutual labels:  browserify
biguint-format
Node.js module to format big uint numbers from a byte array or a Buffer
Stars: ✭ 16 (-75%)
Mutual labels:  buffer
bipbuffer
a C implementation of Simon Cooke's bipbuffer
Stars: ✭ 15 (-76.56%)
Mutual labels:  buffer
Pulse
❤️ A heart rate camera pulse detector written in Swift.
Stars: ✭ 53 (-17.19%)
Mutual labels:  buffer
vivid.ex
Vivid is a simple 2D rendering library written in Elixir.
Stars: ✭ 27 (-57.81%)
Mutual labels:  buffer
ember-validated-form-buffer
A validated form buffer that wraps Ember Data models for use in forms.
Stars: ✭ 46 (-28.12%)
Mutual labels:  buffer
as-string-sink
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript
Stars: ✭ 23 (-64.06%)
Mutual labels:  buffer
g5-component
Event based Browserify component scaffold. ⚾
Stars: ✭ 15 (-76.56%)
Mutual labels:  browserify
idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-76.56%)
Mutual labels:  browserify
codec
Encode keys, values and range options, with built-in or custom encodings.
Stars: ✭ 27 (-57.81%)
Mutual labels:  buffer
sitio
imperative static site generator powered by React and browserify
Stars: ✭ 49 (-23.44%)
Mutual labels:  browserify
node-pg-large-object
Large object support for PostgreSQL clients using the node-postgres library.
Stars: ✭ 31 (-51.56%)
Mutual labels:  buffer
bubleify
Browserify transform that compiles es2015 to es5 using Bublé
Stars: ✭ 21 (-67.19%)
Mutual labels:  browserify
gupack
基于gulp的前端构建工具
Stars: ✭ 13 (-79.69%)
Mutual labels:  browserify
RingBuffer
模仿 kfifo 实现的环形缓冲区
Stars: ✭ 64 (+0%)
Mutual labels:  buffer
go-disk-buffer
This package helps to work with huge amount of data, which cannot be stored in RAM
Stars: ✭ 39 (-39.06%)
Mutual labels:  buffer
browserify-persist-fs
Efficient & stable persistent filesystem cache for browserify
Stars: ✭ 16 (-75%)
Mutual labels:  browserify
cypress-browserify-preprocessor
Cypress preprocessor for bundling JavaScript via browserify
Stars: ✭ 23 (-64.06%)
Mutual labels:  browserify
to-ico
Convert PNG to ICO in memory
Stars: ✭ 115 (+79.69%)
Mutual labels:  buffer

typedarray-to-buffer travis npm downloads javascript style guide

Convert a typed array to a Buffer without a copy.

saucelabs

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 a typed array like Uint8Array to work with and you need to convert it to a Buffer. What do you do?

Of course: Buffer.from(uint8array)

But, alas, every time you do Buffer.from(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!

If you have an ArrayBuffer, you don't need this module, because Buffer.from(arrayBuffer) is already efficient.

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 is a buffer now!

arr.toString()  // '\u0001\u0002\u0003'
arr.readUInt16BE(0)  // 258

how it works

If the browser supports typed arrays, then toBuffer will augment the typed array you pass in with the Buffer methods and return it. See how does Buffer work? for more about how augmentation works.

This module uses the typed array's underlying ArrayBuffer to back the new Buffer. This respects the "view" on the ArrayBuffer, i.e. byteOffset and byteLength. In other words, if you do toBuffer(new Uint32Array([1, 2, 3])), then the new Buffer will contain [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0], not [1, 2, 3]. And it still doesn't require a copy.

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 Buffer.from. This is just for the convenience of modules that work in both node and the browser.

license

MIT. Copyright (C) Feross Aboukhadijeh.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].