Already knowing that ArrayBuffer
represents binary data in browser memory (a collection of fixed size bytes), typed arrays and DataView
allow us to handle this ArrayBuffer
collection: just read or modify numbers about the bytes in that collection.
In addition to DataView
can represent numbers of different types in a single ArrayBuffer
, the difference is only how the bytes that form each number are ordered in the representation offered by the interface (endianness ). For example, we will decompose the bytes of a number into 16 bits (2 bytes) into two different orders (hexadecimal representation):
- (no big-endian)
01 00
- (or little-endian):
00 01
In these 2 orders (the best known), a reverse of the other. The endianness only applies to the bytes that form the number itself.
The DataView
defines the bytes of a number regardless of the endianness device / platform, always using big-endian (or "network-endianness"). It is said that this is useful because of internet broadcasts / network.
Even using DataView
it is still possible to apply the platform's current endianness: simply set the number manually using 8-bit integers (1 byte), but it is easier to use Uint8Array
then.
The numbers of typed arrays Int8Array
and Uint8Array
are the only numbers that are not affected by endianness, because they use only 1 byte of ArrayBuffer
data to be represented.
console.log(
'Uint8Array:
${new Uint8Array([ 0 ]).buffer.byteLength}'
, '\nInt8Array:
${new Int8Array([ -129 ]).buffer.byteLength}'
);
This gives a basic idea, "if DataView
is worth more than typed arrays".
On performance I do not know much: |, it's just said that it's more efficient to manage the platform's current endianness (the fastest for it), it should not cost much.