Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Buffer Allocation and Encoding in Node.js
📖 Scenario: You are working on a Node.js application that needs to handle text data efficiently. You want to learn how to create buffers, allocate memory, and encode strings into buffers.
🎯 Goal: Build a simple Node.js script that allocates a buffer, encodes a string into it using UTF-8 encoding, and prepares it for further processing.
📋 What You'll Learn
Create a buffer with a fixed size
Define a string to encode
Encode the string into the buffer using UTF-8 encoding
Add a final step to confirm the buffer contains the encoded data
💡 Why This Matters
🌍 Real World
Buffers are used in Node.js to handle binary data efficiently, such as reading files, working with network data, or encoding text.
💼 Career
Understanding buffer allocation and encoding is essential for backend developers working with Node.js, especially when dealing with file systems, streams, or network protocols.
Progress0 / 4 steps
1
Create a buffer with fixed size
Create a buffer called buffer with a size of 20 bytes using Buffer.alloc().
Node.js
Hint
Use Buffer.alloc(20) to create a buffer of 20 bytes.
2
Define a string to encode
Create a constant string called text and set it to 'Hello Node.js'.
Node.js
Hint
Use const text = 'Hello Node.js'; to define the string.
3
Encode the string into the buffer
Use buffer.write() to encode the string text into buffer using 'utf8' encoding.
Node.js
Hint
Call buffer.write(text) to encode the string.
4
Confirm buffer contains encoded data
Add a line to export the buffer variable using module.exports = buffer; so it can be used elsewhere.
Node.js
Hint
Use module.exports = buffer; to export the buffer.
Practice
(1/5)
1. What does Buffer.alloc(5) do in Node.js?
easy
A. Creates a buffer of length 5 filled with zeros
B. Creates a buffer of length 5 filled with random data
C. Creates a buffer from a string of length 5
D. Allocates memory but does not initialize the buffer
Solution
Step 1: Understand Buffer.alloc behavior
Buffer.alloc(size) creates a buffer of the given size and fills it with zeros for safety.
Step 2: Apply to size 5
Calling Buffer.alloc(5) creates a buffer of length 5 filled with zeros.
Final Answer:
Creates a buffer of length 5 filled with zeros -> Option A
Quick Check:
Buffer.alloc(5) = zero-filled buffer [OK]
Hint: Buffer.alloc always zero-fills the buffer size [OK]
Common Mistakes:
Thinking Buffer.alloc fills with random data
Confusing Buffer.alloc with Buffer.from
Assuming buffer is uninitialized
2. Which of the following is the correct syntax to create a buffer from the string 'hello' using UTF-8 encoding?
easy
A. Buffer.alloc('hello', 'utf8')
B. Buffer.create('hello', 'utf8')
C. new Buffer('hello', 'utf8')
D. Buffer.from('hello', 'utf8')
Solution
Step 1: Identify correct Buffer creation method
In modern Node.js, Buffer.from() creates a buffer from a string with encoding.
Step 2: Check syntax correctness
Buffer.from('hello', 'utf8') is correct syntax; Buffer.alloc does not accept string input, and Buffer.create does not exist. new Buffer() is deprecated.
Final Answer:
Buffer.from('hello', 'utf8') -> Option D
Quick Check:
Use Buffer.from for string buffers [OK]
Hint: Use Buffer.from for strings, Buffer.alloc for size [OK]
Buffer.alloc(size, fill) accepts a size and a fill value. The fill can be a string, which repeats to fill the buffer.
Step 2: Understand fill behavior and toString()
Filling 5 bytes with 'abc' repeats 'abcab'. toString() converts buffer back to string.
Final Answer:
No error, prints 'abcab' -> Option C
Quick Check:
Buffer.alloc fills string repeatedly [OK]
Hint: Buffer.alloc fills string repeatedly if fill is string [OK]
Common Mistakes:
Thinking Buffer.alloc fill cannot be string
Assuming toString() is invalid on Buffer
Expecting error due to fill length mismatch
5. You want to create a buffer from the string 'café' and then convert it back to a string. Which encoding should you use to preserve the accented character correctly?
hard
A. 'ascii'
B. 'utf8'
C. 'base64'
D. 'hex'
Solution
Step 1: Understand character encoding for accented characters
ASCII encoding cannot represent accented characters like 'é'. UTF-8 supports all Unicode characters including accented ones.
Step 2: Choose encoding for buffer creation and conversion
Using 'utf8' ensures the accented character is preserved when converting to and from buffer.
Final Answer:
'utf8' -> Option B
Quick Check:
Use UTF-8 for accented characters [OK]
Hint: Use UTF-8 to handle accented characters correctly [OK]
Common Mistakes:
Using ASCII which drops accents
Using base64 or hex which are encoding formats, not text encodings