Binary data in Node.js: encodings, byte order, and shared memory
How Buffer, Uint8Array, ArrayBuffer, and DataView relate, how encodings and byte order work, which operations share memory, and why turning binary data into a string destroys it. We ran every code sample in Node to check the answers.
Questions
- Not answered.
Bufferis a subclass of which built-in JavaScript typed array? - Not answered. What is an
ArrayBuffer, and how do you read the bytes inside it? - Not answered. What is
new Uint32Array(4).byteLength? - Not answered. Which statements about
BufferversusUint8Arrayare correct? - Not answered. How many bytes is the string
caféin UTF-8? - Not answered. What does
Buffer.from('SGVsbG8=', 'base64').toString('utf8')return? - Not answered. You read a binary file with
let data = ''andstream.on('data', c => data += c). Why is the result corrupted? - Not answered. Decoding a UTF-8 stream with
chunk.toString()on each chunk sometimes produces�. Why? - Not answered. What is
[...new Uint8Array([256, -1, 300])]? - Not answered. Read as a 32-bit integer, the bytes
00 00 00 01give1one way and16777216the other. What causes the difference? - Not answered. Which of these operations return something that shares memory with the source instead of copying it?
- Not answered. What is the difference between
Buffer.alloc(1024)andBuffer.allocUnsafe(1024)?