0
0
Node.jsframework~5 mins

Buffer allocation and encoding in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ABuffer.create(size)
BBuffer.allocUnsafe(size)
CBuffer.from(size)
DBuffer.alloc(size)
What does Buffer.allocUnsafe(size) do?
ACreates a Buffer with uninitialized memory (old data)
BCreates a Buffer filled with zeros
CCreates a Buffer from a string
DThrows an error
How do you convert a string 'hello' to a Buffer with UTF-8 encoding?
ABuffer.alloc('hello')
BBuffer.encode('hello')
CBuffer.from('hello', 'utf8')
DBuffer.toString('hello')
Which encoding is NOT commonly used with Buffers?
Ajson
Bbase64
Cutf8
Dhex
Why should you be careful using Buffer.allocUnsafe()?
AIt fills Buffer with zeros
BIt may contain old sensitive data until overwritten
CIt is slower than Buffer.alloc()
DIt does not create a Buffer
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.