Code Monkey home page Code Monkey logo

typedarray-to-buffer's Introduction

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.

typedarray-to-buffer's People

Contributors

feross avatar greenkeeper[bot] avatar greenkeeperio-bot avatar linusu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

typedarray-to-buffer's Issues

Republish with updated NPM

The version of module which is currently published in npm registry appears to be affected by this npm bug:
npm/npm#16734

Which prevents the application using this module (indirectly) from being deployed on Amazon AWS:
aws/aws-cli#2639

Republishing the module using newer (or stable) version of npm should resolve the issue.

Reduce size/dependencies?

You could remove is-typedarray?
and use offset+length from the start instead of slicing afterwards

module.exports = function typedarrayToBuffer (arr) {
  return ArrayBuffer.isView(arr)
    ? Buffer.from(arr.buffer, arr.byteOffset, arr.byteOffset + arr.byteLength)
    : Buffer.from(arr)
}

An in-range update of tape is breaking the build 🚨

Version 4.9.1 of tape was just published.

Branch Build failing 🚨
Dependency tape
Current Version 4.9.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

tape is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 10 commits.

  • 050b318 v4.9.1
  • 73232c0 [Dev Deps] update js-yaml
  • 8a2d29b [Deps] update has, for-each, resolve, object-inspect
  • c6f5313 [Tests] add eclint and eslint, to enforce a consistent style
  • 45788a5 [Dev Deps] update concat-stream
  • ec4a71d [fix] Fix bug in functionName regex during stack parsing
  • 7261ccc Merge pull request #433 from mcnuttandrew/add-trb
  • 6cbc53e Add tap-react-browser
  • 9d501ff [Dev Deps] use ~ for dev deps; update to latest nonbreaking
  • 24e0a8d Fix spelling of "parameterize"

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Support for float32Array?

Any chance this can be enhanced to accept float32 arrays as an input before converting these to buffers?

Version 3.1.4 package timestamps are incorrect

When installing the latest version of this package through npm, a number of files have a timestamp of Jan 1 1970:

-rw-r--r-- 1 root root  758 Jan  1  1970 index.js
-rw-r--r-- 1 root root 1081 Jan  1  1970 LICENSE
drwxr-xr-x 3 root root 4096 Mar  6 10:10 node_modules
-rw-r--r-- 1 root root 2179 Mar  6 10:10 package.json
-rw-r--r-- 1 root root 3463 Jan  1  1970 README.md
drwxr-xr-x 2 root root 4096 Jan  1  1970 test

In my use case, this is causing issues when trying to package a project with this dependency using Python zip (raise ValueError('ZIP does not support timestamps before 1980').

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.