Beginner LevelNode JS
What is a Node.js Buffer and how do Buffer.alloc() and Buffer.from() handle raw binary data?
❓ Interview Prompt: Explain Node.js Buffer class, raw binary memory outside V8 heap, Buffer.alloc(), Buffer.from(), and encoding conversions.
A Buffer is a fixed-length sequence of bytes allocated in raw memory outside the V8 JavaScript heap:
Creating Buffers:
•
'Buffer.alloc(size)': Allocates zero-filled safe memory buffer of specified byte size.
•
'Buffer.from(data, encoding)': Creates a buffer containing copies of string/array binary data ('utf8', 'hex', 'base64').
Code Implementation Example
javascript
// Allocate 10 bytes of zero-filled memory
const buf1 = Buffer.alloc(10);
// Create buffer from UTF-8 string
const buf2 = Buffer.from('Hello Node.js', 'utf8');
// Convert binary buffer to Base64 string
const base64Str = buf2.toString('base64');
console.log('Hex:', buf2.toString('hex'));
console.log('Base64:', base64Str);💡Creating and converting binary Buffers in Node.js.
🎯 Interview Key Takeaway
Buffers represent fixed-size raw binary memory allocated outside the V8 engine heap for network and file I/O.
#Buffer#Binary Data#base64#utf8#Beginner
Back to all Node JS Questions→