What is Buffer in Node.js ?
Last Updated :
16 Oct, 2024
In Node, Buffer is used to store and manage binary data. Pure JavaScript is great with Unicode-encoded strings, but it does not handle binary data very well. It is not problematic when we perform an operation on data at the browser level but at the time of dealing with TCP stream and performing a read-write operation on the file system is required to deal with pure binary data.
To satisfy this need Node.js uses Buffer to handle the binary data. So in this article, we are going to know about buffer in Node.js.
What is Buffer in Node?
Buffer in Node is a built-in object used to perform operations on raw binary data. The buffer class allows us to handle the binary data directly.
Syntax:
const buf = Buffer.alloc(10); // Allocates a buffer of 10 bytes.
Generally, Buffer refers to the particular memory location in memory. Buffer and array have some similarities, but the difference is array can be any type, and it can be resizable. Buffers only deal with binary data, and it can not be resizable. Each integer in a buffer represents a byte. console.log() function is used to print the Buffer instance.
Buffer Methods:
No | Method | Description |
---|
1 | Buffer.alloc(size) | It creates a buffer and allocates size to it. |
---|
2 | Buffer.from(initialization) | It initializes the buffer with given data. |
---|
3 | Buffer.write(data) | It writes the data on the buffer. |
---|
4 | toString() | It read data from the buffer and returned it. |
---|
5 | Buffer.isBuffer(object) | It checks whether the object is a buffer or not. |
---|
6 | Buffer.length | It returns the length of the buffer. |
---|
7 | Buffer.copy(buffer,subsection size) | It copies data from one buffer to another. |
---|
8 | Buffer.slice(start, end=buffer.length) | It returns the subsection of data stored in a buffer. |
---|
9 | Buffer.concat([buffer,buffer]) | It concatenates two buffers. |
---|
Example: Basic Implementation of Node.js Buffers
Node
// Filename: index.js
// Different Method to create Buffer
const buffer1 = Buffer.alloc(100);
const buffer2 = new Buffer('GFG');
const buffer3 = Buffer.from([1, 2, 3, 4]);
// Writing data to Buffer
buffer1.write("Happy Learning");
// Reading data from Buffer
const a = buffer1.toString('utf-8');
console.log(a);
// Check object is buffer or not
console.log(Buffer.isBuffer(buffer1));
// Check length of Buffer
console.log(buffer1.length);
// Copy buffer
const bufferSrc = new Buffer('ABC');
const bufferDest = Buffer.alloc(3);
bufferSrc.copy(bufferDest);
const Data = bufferDest.toString('utf-8');
console.log(Data);
// Slicing data
const bufferOld = new Buffer('GeeksForGeeks');
const bufferNew = bufferOld.slice(0, 4);
console.log(bufferNew.toString());
// concatenate two buffer
const bufferOne = new Buffer('Happy Learning ');
const bufferTwo = new Buffer('With GFG');
const bufferThree = Buffer.concat([bufferOne, bufferTwo]);
console.log(bufferThree.toString());
Run the index.js file using the following command:
node index.js
Output:
Happy Learning
true
100
ABC
Geek
Happy Learning With GFG
Buffers are highly efficient when dealing with large amounts of binary data or when performance is critical, as they bypass the overhead of encoding/decoding data into JavaScript's native string format.
Reference: https://nodejs.org/api/buffer.html
Similar Reads
What are Buffers in Node.js ? Buffers are an essential concept in Node.js, especially when working with binary data streams such as files, network protocols, or image processing. Unlike JavaScript, which is typically used to handle text-based data, Node.js provides buffers to manage raw binary data. This article delves into what
4 min read
What is Chunk in Node.js ? In Node.js, the term "chunk" refers to a small, manageable piece of data that is part of a larger dataset. Node.js processes data in chunks, especially when dealing with streams, which makes handling large files or data sources more efficient and memory-friendly. This article explores what chunks ar
4 min read
Node.js Buffers Node.js Buffers are used to handle binary data directly in memory. They provide a way to work with raw binary data streams efficiently, crucial for I/O operations, such as reading from files or receiving data over a network.Buffers in Node.jsBuffers are instances of the Buffer class in Node.js. Buff
4 min read
What is Piping in Node.js ? Piping in NodeJS is the process by which byte data from one stream is sent to another stream. It is a powerful feature in Node.js that allows data to be transferred from one stream to another seamlessly. Streams are an integral part of Node.js, providing a mechanism for handling data that is too lar
6 min read
Node.js Buffer.entries() Method The Buffer.entries() method is used to create and return the iterator of [index, byte] pairs from the contents of the buffer. Syntax: Buffer.entries() Parameters: This method does not accept any parameters. Return value: This method returns an Iterator object of pair in [index, byte] format. Exam
2 min read