Loading lesson path
Concept visual
Start from A
The Buffer module in Node.js is used to handle binary data. Buffers are similar to arrays of integers but are fixed-length and correspond to raw memory allocations outside the V8 JavaScript engine.
Node.js provides the Buffer class as a global object, so you don't need to require or import it explicitly.Since Node.js v6.0.0, the Buffer constructor is deprecated in favor of the new Buffer methods. Using the constructor could lead to security vulnerabilities due to uninitialized memory.
Buffers in Node.js are used to handle binary data directly. They are similar to arrays of integers but are fixed in size and represent raw memory allocations outside the V8 heap.
// Create a buffer from a string const buf = Buffer.from('Hello, Node.js!');
// Buffers can be converted to strings console.log(buf.toString()); // 'Hello, Node.js!'
// Access individual bytes console.log(buf[0]); // 72 (ASCII for 'H')
// Buffers have a fixed length console.log(buf.length); // 15There are several ways to create buffers in Node.js, each with different performance and safety characteristics: There are several ways to create buffers in Node.js:
// Create a buffer of 10 bytes filled with zeros const buffer1 = Buffer.alloc(10);
console.log(buffer1);Buffer.alloc() but may contain old or sensitive data. Always fill the buffer before use if security is a concern.
// Create an uninitialized buffer of 10 bytes const buffer2 = Buffer.allocUnsafe(10);
console.log(buffer2);
// Fill the buffer with zeros for security buffer2.fill(0);
console.log(buffer2);Buffer.allocUnsafe() is faster than Buffer.alloc() but can expose sensitive data. Only use it when you understand the security implications and plan to immediately fill the entire buffer.
// Create a buffer from a string const buffer3 = Buffer.from('Hello, World!');
console.log(buffer3);
console.log(buffer3.toString());
// Create a buffer from an array of integers const buffer4 = Buffer.from([65, 66, 67, 68, 69]);
console.log(buffer4);
console.log(buffer4.toString());
// Create a buffer from another buffer const buffer5 = Buffer.from(buffer4);
console.log(buffer5);You can write data to a buffer using various methods:
// Create an empty buffer const buffer = Buffer.alloc(10);
// Write a string to the buffer buffer.write('Hello');
console.log(buffer);
console.log(buffer.toString());
// Write bytes at specific positions buffer[5] = 44; // ASCII for ','
buffer[6] = 32; // ASCII for space buffer.write('Node', 7);
console.log(buffer.toString());You can read data from a buffer using various methods:
// Create a buffer from a string const buffer = Buffer.from('Hello, Node.js!');
// Read the entire buffer as a string console.log(buffer.toString());
// Read a portion of the buffer (start at position 7, end before position 11)
console.log(buffer.toString('utf8', 7, 11));
// Read a single byte console.log(buffer[0]);
// Convert the ASCII code to a character console.log(String.fromCharCode(buffer[0]));Buffers can be iterated like arrays:
// Create a buffer from a string const buffer = Buffer.from('Hello');
// Iterate using for...of loop for (const byte of buffer) {
console.log(byte);
}
// Iterate using forEach buffer.forEach((byte, index) => {
console.log(`Byte at position ${index}: ${byte}`);
});Buffer.compare() Compares two buffers and returns a number indicating whether the first one comes before, after, or is the same as the second one in sort order:
const buffer1 = Buffer.from('ABC');
const buffer2 = Buffer.from('BCD');
const buffer3 = Buffer.from('ABC');
console.log(Buffer.compare(buffer1, buffer2));
console.log(Buffer.compare(buffer2, buffer1));
console.log(Buffer.compare(buffer1, buffer3));
buffer.copy()Copies data from one buffer to another:
// Create source and target buffers const source = Buffer.from('Hello, World!');
const target = Buffer.alloc(source.length);
// Copy from source to target source.copy(target);
console.log(target.toString());
// Create a target buffer for partial copy const partialTarget = Buffer.alloc(5);
// Copy only part of the source (starting at index 7)
source.copy(partialTarget, 0, 7);
console.log(partialTarget.toString());
buffer.slice()Creates a new buffer that references the same memory as the original, but with offset and cropped to the given end:
const buffer = Buffer.from('Hello, World!');
// Create a slice from position 7 to the end const slice = buffer.slice(7);
console.log(slice.toString());
// Create a slice from position 0 to 5 const slice2 = buffer.slice(0, 5);
console.log(slice2.toString());
// Important: slices share memory with original buffer slice[0] = 119; // ASCII for 'w' (lowercase)
console.log(slice.toString());
console.log(buffer.toString());Since buffer.slice() creates a view of the same memory, modifying either the original buffer or the slice will affect the other. buffer.toString() Decodes a buffer to a string using a specified encoding:
const buffer = Buffer.from('Hello, World!');
// Default encoding is UTF-8 console.log(buffer.toString());
// Specify encoding console.log(buffer.toString('utf8'));
// Decode only a portion of the buffer console.log(buffer.toString('utf8', 0, 5));
// Using different encodings const hexBuffer = Buffer.from('48656c6c6f', 'hex');
console.log(hexBuffer.toString());
const base64Buffer = Buffer.from('SGVsbG8=', 'base64');
console.log(base64Buffer.toString());
buffer.equals()Compares two buffers for content equality:
const buffer1 = Buffer.from('Hello');
const buffer2 = Buffer.from('Hello');
const buffer3 = Buffer.from('World');
console.log(buffer1.equals(buffer2));
console.log(buffer1.equals(buffer3));
console.log(buffer1 === buffer2);Buffers work with various encodings when converting between strings and binary data:
// Create a string const str = 'Hello, World!';
// Convert to different encodings const utf8Buffer = Buffer.from(str, 'utf8');
console.log('UTF-8:', utf8Buffer);
const base64Str = utf8Buffer.toString('base64');
console.log('Base64 string:', base64Str);
const hexStr = utf8Buffer.toString('hex');
console.log('Hex string:', hexStr);
// Convert back to original const fromBase64 = Buffer.from(base64Str, 'base64').toString('utf8');
console.log('From Base64:', fromBase64);
const fromHex = Buffer.from(hexStr, 'hex').toString('utf8');
console.log('From Hex:', fromHex);Supported encodings in Node.js include: utf8
Formula
: Multi - byte encoded Unicode characters (default)ascii
Formula
: ASCII characters only (7 - bit)latin1
Formula
: Latin - 1 encoding (ISO 8859 - 1)base64 : Base64 encoding hex : Hexadecimal encoding binary : Binary encoding (deprecated)
Formula
ucs2/utf16le
: 2 or 4 bytes, little - endian encoded Unicode charactersYou can combine multiple buffers into one using Buffer.concat()
const buf1 = Buffer.from('Hello, ');
const buf2 = Buffer.from('Node.js!');
// Concatenate buffers const combined = Buffer.concat([buf1, buf2]);
console.log(combined.toString()); // 'Hello, Node.js!'
// With a maximum length parameter const partial = Buffer.concat([buf1, buf2], 5);
console.log(partial.toString()); // 'Hello'Buffers provide methods to search for values or sequences:
const buf = Buffer.from('Hello, Node.js is awesome!');
// Find the first occurrence of a value console.log(buf.indexOf('Node')); // 7
// Check if buffer contains a value console.log(buf.includes('awesome')); // true
// Find the last occurrence of a value console.log(buf.lastIndexOf('e')); // 24Buffers are commonly used with streams for efficient data processing:
const fs = require('fs');
const { Transform } = require('stream');
// Create a transform stream that processes data in chunks const transformStream = new Transform({
transform(chunk, encoding, callback) {
// Process each chunk (which is a Buffer)
const processed = chunk.toString().toUpperCase();
this.push(Buffer.from(processed));
callback();
}
});
// Create a read stream from a file const readStream = fs.createReadStream('input.txt');
// Create a write stream to a file const writeStream = fs.createWriteStream('output.txt');
// Process the file in chunks readStream.pipe(transformStream).pipe(writeStream);Buffers are commonly used for file system operations:
const fs = require('fs');
// Write buffer to file const writeBuffer = Buffer.from('Hello, Node.js!');
fs.writeFile('buffer.txt', writeBuffer, (err) => {
if (err) throw err;
console.log('File written successfully');
// Read file into buffer fs.readFile('buffer.txt', (err, data) => {
if (err) throw err;
// 'data' is a buffer console.log('Read buffer:', data);
console.log('Buffer content:', data.toString());
// Read only part of the file into a buffer const smallBuffer = Buffer.alloc(5);
fs.open('buffer.txt', 'r', (err, fd) => {
if (err) throw err;
// Read 5 bytes starting at position 7 fs.read(fd, smallBuffer, 0, 5, 7, (err, bytesRead, buffer) => {
if (err) throw err;
console.log('Partial read:', buffer.toString());// Output: Node.
fs.close(fd, (err) => {
if (err) throw err;
});
});
});
});
});Buffers consume memory outside the JavaScript heap, which can be both an advantage (less garbage collection pressure) and a disadvantage (must be carefully managed)