The Buffer class in Node.js provides instances to store raw binary data allocated outside the V8 JavaScript heap in fixed-length, contiguous memory arrays. Buffers are essential when dealing with file streams, network packets, and cryptographic keys.
flowchart LR
A["Raw Binary Stream / Disk Data"] --> B["Fixed-Size Buffer Allocation (C++ Memory Heap)"]
B --> C["Encodings: UTF-8 / Hex / Base64 / Binary"]
C --> D["JavaScript String & Array Views"]
import { Buffer } from 'node:buffer';
// 1. Allocating buffers
const buf1 = Buffer.alloc(10); // 10 bytes initialized with zeros
const buf2 = Buffer.from('Hello Node.js Buffer!', 'utf-8');
console.log(`Buffer 2 Byte Length: ${buf2.length}`); // 21 bytes
console.log('Hex representation:', buf2.toString('hex'));
console.log('Base64 representation:', buf2.toString('base64'));
// 2. Modifying buffer content
const mutableBuf = Buffer.alloc(5);
mutableBuf.write('Node');
console.log('Written Buffer:', mutableBuf.toString('utf-8'));
// 3. Concatenating Buffers
const part1 = Buffer.from('Kodersolution ');
const part2 = Buffer.from('Developer Portal');
const combinedBuffer = Buffer.concat([part1, part2]);
console.log('Combined Output:', combinedBuffer.toString('utf-8'));
// 4. Slicing Buffers (shares underlying memory array)
const sliceBuf = combinedBuffer.subarray(0, 13);
console.log('Subarray Slice:', sliceBuf.toString('utf-8')); // 'Kodersolution'
Buffer.alloc() Over Buffer.allocUnsafe(): Buffer.alloc() initializes memory with zero filled bytes; allocUnsafe() is faster but leaves uninitialized memory containing sensitive past data.Buffer.byteLength() vs .length: Use Buffer.byteLength(str) to calculate string byte size accurately, because multi-byte UTF-8 characters (like emojis) take more bytes than string character length.subarray(): buf.subarray() returns a view over the original buffer without copying bytes; mutating the slice alters original buffer data.Convert the ASCII text string "Node.js Security" into a hexadecimal string using Node.js Buffer.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With