typedarray-to-buffer

WebJar for typedarray-to-buffer

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

typedarray-to-buffer
Last Version

Last Version

3.1.5
Release Date

Release Date

Type

Type

jar
Description

Description

typedarray-to-buffer
WebJar for typedarray-to-buffer
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/feross/typedarray-to-buffer

Download typedarray-to-buffer

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/typedarray-to-buffer/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>typedarray-to-buffer</artifactId>
    <version>3.1.5</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/typedarray-to-buffer/
implementation 'org.webjars.npm:typedarray-to-buffer:3.1.5'
// https://jarcasting.com/artifacts/org.webjars.npm/typedarray-to-buffer/
implementation ("org.webjars.npm:typedarray-to-buffer:3.1.5")
'org.webjars.npm:typedarray-to-buffer:jar:3.1.5'
<dependency org="org.webjars.npm" name="typedarray-to-buffer" rev="3.1.5">
  <artifact name="typedarray-to-buffer" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='typedarray-to-buffer', version='3.1.5')
)
libraryDependencies += "org.webjars.npm" % "typedarray-to-buffer" % "3.1.5"
[org.webjars.npm/typedarray-to-buffer "3.1.5"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : is-typedarray jar [1.0.0,2)

Project Modules

There are no modules declared in this project.

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.

Versions

Version
3.1.5
3.1.2