0
0
Node.jsframework~10 mins

Buffer allocation and encoding in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Buffer allocation and encoding
Start
Allocate Buffer
Write Data with Encoding
Read or Use Buffer
End
This flow shows how Node.js allocates a buffer, writes data with a specific encoding, then reads or uses the buffer.
Execution Sample
Node.js
const buf = Buffer.alloc(5);
buf.write('Hi', 'utf8');
console.log(buf);
console.log(buf.toString('utf8'));
Allocates a 5-byte buffer, writes 'Hi' in UTF-8 encoding, then prints the buffer and its string form.
Execution Table
StepActionBuffer Content (hex)Buffer Content (string)Notes
1Allocate buffer of length 500 00 00 00 00Buffer initialized with zeros
2Write 'Hi' with utf8 encoding48 69 00 00 00Hi'H' = 0x48, 'i' = 0x69, rest zeros
3Print buffer object<Buffer 48 69 00 00 00>Shows raw bytes in hex
4Convert buffer to string utf848 69 00 00 00HiDecodes entire buffer; null bytes (\u0000) included but non-printable in console
5EndProcess complete
💡 Buffer allocated fixed size; write fills bytes; toString decodes entire buffer by default (null bytes included)
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
bufundefined<Buffer 00 00 00 00 00><Buffer 48 69 00 00 00><Buffer 48 69 00 00 00><Buffer 48 69 00 00 00>
Key Moments - 3 Insights
Why does the buffer show zeros after the written characters?
Because the buffer was allocated with a fixed size of 5 bytes, and only the first 2 bytes were written with 'Hi'. The remaining 3 bytes remain zero as shown in step 2 of the execution table.
Why does buf.toString('utf8') output 'Hi' and not include the trailing zeros?
The toString method decodes the entire buffer as UTF-8, resulting in 'Hi\u0000\u0000\u0000'. Null bytes (\u0000) are non-printable, so they do not visibly appear in console output (step 4).
What happens if you write a string longer than the buffer size?
Only the bytes that fit in the allocated buffer are written; the rest are ignored. This prevents buffer overflow but truncates the string.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the buffer content in hex after writing 'Hi'?
A00 00 00 00 00
B48 69 00 00 00
C48 69 48 69 00
D00 48 69 00 00
💡 Hint
Check step 2 in the execution table for buffer content after write
At which step does the buffer get allocated with zeros?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the first step where the buffer is created
If you write 'HelloWorld' into the 5-byte buffer, what happens?
ABuffer contains 'Hello' and ignores the rest
BBuffer expands automatically to fit all
CBuffer throws an error
DBuffer contains 'World'
💡 Hint
Recall buffer size is fixed at allocation (step 1) and write truncates excess
Concept Snapshot
Buffer.alloc(size) creates a fixed-size buffer filled with zeros.
Use buf.write(string, encoding) to write data into the buffer.
Writing beyond buffer size truncates the string.
buf.toString(encoding) decodes the entire buffer as string by default; null bytes become \u0000.
Buffers hold raw bytes, useful for binary data handling in Node.js.
Full Transcript
This lesson shows how Node.js buffers are allocated and used. First, a buffer of fixed size is created with Buffer.alloc, filled with zeros. Then, writing a string like 'Hi' with UTF-8 encoding fills the first bytes of the buffer. The rest remain zero. When printing the buffer, you see the raw bytes in hex. Converting the buffer to a string decodes all bytes; trailing null bytes are included but invisible when printed. Writing a string longer than the buffer size only writes what fits, truncating the rest. This helps manage binary data safely and efficiently.