Uint8Arrays bring memory-efficient(ish) byte handling to browsers - they are similar to Node.js Buffers but lack a lot of the utility methods present on that class.
This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc.
Since Node.js Buffers are also Uint8Arrays, it falls back to Buffer internally where it makes sense for performance reasons.
Uint8Array
s bring memory-efficient(ish) byte handling to browsers - they are similar to Node.jsBuffer
s but lack a lot of the utility methods present on that class.This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc.
Since Node.js
Buffer
s are alsoUint8Array
s, it falls back toBuffer
internally where it makes sense for performance reasons.alloc(size)
Create a new
Uint8Array
. When running under Node.js,Buffer
will be used in preference toUint8Array
.Example
allocUnsafe(size)
Create a new
Uint8Array
. When running under Node.js,Buffer
will be used in preference toUint8Array
.On platforms that support it, memory referenced by the returned
Uint8Array
will not be initialized.Example
compare(a, b)
Compare two
Uint8Arrays
Example
concat(arrays, [length])
Concatenate one or more
Uint8Array
s and return aUint8Array
with their contents.If you know the length of the arrays, pass it as a second parameter, otherwise it will be calculated by traversing the list of arrays.
Example
equals(a, b)
Returns true if the two arrays are the same array or if they have the same length and contents.
Example
fromString(string, encoding = 'utf8')
Returns a new
Uint8Array
created from the passed string and interpreted as the passed encoding.Supports
utf8
and any of the multibase encodings as implemented by the multiformats module.Example
toString(array, encoding = 'utf8')
Returns a string created from the passed
Uint8Array
in the passed encoding.Supports
utf8
and any of the multibase encodings as implemented by the multiformats module.Example
xor(a, b)
Returns a
Uint8Array
containinga
andb
xored together.Example
xorCompare(a, b)
Compares the distances between two xor
Uint8Array
s.Example