Gathering detailed insights and metrics for typedarray-to-buffer
Gathering detailed insights and metrics for typedarray-to-buffer
Gathering detailed insights and metrics for typedarray-to-buffer
Gathering detailed insights and metrics for typedarray-to-buffer
Convert a typed array to a Buffer without a copy.
npm install typedarray-to-buffer
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
66 Stars
93 Commits
16 Forks
7 Watching
5 Branches
3 Contributors
Updated on 23 Oct 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-10.9%
3,415,641
Compared to previous day
Last week
1.3%
20,977,197
Compared to previous week
Last month
22.4%
82,028,603
Compared to previous month
Last year
-6.8%
815,542,949
Compared to previous year
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.
1npm install typedarray-to-buffer
To convert a typed array to a Buffer
without a copy, do this:
1var toBuffer = require('typedarray-to-buffer')
2
3var arr = new Uint8Array([1, 2, 3])
4arr = toBuffer(arr)
5
6// arr is a buffer now!
7
8arr.toString() // '\u0001\u0002\u0003'
9arr.readUInt16BE(0) // 258
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.
MIT. Copyright (C) Feross Aboukhadijeh.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/24 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More