Recall & Review
beginner
What is a Buffer in Node.js?
A Buffer is a special object in Node.js used to store raw binary data. It helps handle data like files, network packets, or streams efficiently.
Click to reveal answer
beginner
How do you allocate a Buffer of 10 bytes filled with zeros?
Use Buffer.alloc(10). This creates a Buffer of length 10 filled with zeros safely.
Click to reveal answer
intermediate
What is the difference between Buffer.alloc() and Buffer.allocUnsafe()?
Buffer.alloc() creates a zero-filled Buffer for safety. Buffer.allocUnsafe() creates a Buffer faster but with old data inside, so you must overwrite it before use.
Click to reveal answer
beginner
How do you convert a string to a Buffer with UTF-8 encoding?
Use Buffer.from('your string', 'utf8'). This encodes the string into bytes using UTF-8.
Click to reveal answer
intermediate
What encoding options can you use when creating or converting Buffers?
Common encodings include 'utf8', 'ascii', 'base64', 'hex', and 'latin1'. They tell Node.js how to interpret or convert the bytes.
Click to reveal answer
Which method creates a Buffer filled with zeros?
✗ Incorrect
Buffer.alloc(size) creates a zero-filled Buffer safely.
What does Buffer.allocUnsafe(size) do?
✗ Incorrect
Buffer.allocUnsafe(size) creates a Buffer quickly but with old data inside, so it is unsafe until overwritten.
How do you convert a string 'hello' to a Buffer with UTF-8 encoding?
✗ Incorrect
Buffer.from('hello', 'utf8') converts the string to a Buffer using UTF-8 encoding.
Which encoding is NOT commonly used with Buffers?
✗ Incorrect
'json' is not an encoding for Buffers; common encodings are utf8, base64, hex, ascii, and latin1.
Why should you be careful using Buffer.allocUnsafe()?
✗ Incorrect
Buffer.allocUnsafe() may contain old data, so you must overwrite it before use to avoid leaks.
Explain how to create a Buffer in Node.js and why encoding matters.
Think about how raw data and strings differ and how encoding helps convert between them.
You got /4 concepts.
Describe the risks and benefits of using Buffer.allocUnsafe() compared to Buffer.alloc().
Consider safety versus performance trade-offs.
You got /3 concepts.