Node.js Buffers (original) (raw)

Last Updated : 12 Jul, 2025

**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.js

**Buffers are instances of the Buffer class in Node.js. Buffers are designed to handle binary raw data. Buffers allocate raw memory outside the V8 heap. Buffer class is a global class so it can be used without importing the Buffer module in an application.

Syntax****:**

Following are the different ways to create buffers in Node.js:

**Create an uninitiated buffer: It creates the uninitiated buffer of given size.

const ubuf = Buffer.alloc(5);

The above syntax is used to create an uninitiated buffer of 5 octets.

**Create a buffer from array: It creates the buffer from given array.

const abuf = new Buffer([16, 32, 48, 64]);

The above syntax is used to create a buffer from given array.

**Create a buffer from string: It creates buffer from given string with optional encoding.

var sbuf = new Buffer("GeeksforGeeks", "ascii");

The above syntax is used to create a Buffer from a string and encoding type can also be specified optionally.

**Writing to Buffers in Node.js: The **buf.write() method is used to write data into a node buffer.

buf.write( string, offset, length, encoding )

The **buf.write() method is used to return the number of octets in which string is written. If buffer does not have enough space to fit the entire string, it will write a part of the string.
The buf.write() method accepts the following parameters:

**Example: Create a buffer.js file containing the following codes.

JavaScript `

// Write JavaScript code here cbuf = new Buffer(256); bufferlen = cbuf.write("Learn Programming with GeeksforGeeks!!!"); console.log("No. of Octets in which string is written : "+ bufferlen);

`

**Output:

**Reading from Buffers: The **buf.toString() method is used to read data from a node buffer.
**Syntax:

buf.toString( encoding, start, end )

The buf.toString() method accepts the following parameters:

Explore the Node.js Buffer Complete Reference to uncover detailed explanations, practical usage examples, and expert tips for harnessing its powerful features to efficiently manage and manipulate binary data in your Node.js applications.

Features

**Example 1: Create a buffer.js file containing the following code.

JavaScript `

// Write JavaScript code here rbuf = new Buffer(26); var j;

for (var i = 65, j = 0; i < 90, j < 26; i++, j++) { rbuf[j] = i ; }

console.log( rbuf.toString('ascii'));

`

**Output:

**Example 2: Read the data from Node.js buffer specifying the start and end point of reading. Create a buffer.js file containing the following code.

JavaScript `

// Write JavaScript code here rbuf = new Buffer(26); var j;

for (var i = 65, j = 0; i < 90, j < 26; i++, j++) { rbuf[j] = i ; }

console.log( rbuf.toString('utf-8', 3, 9));

`

**Output:

Benefits

Conclusion

Node.js Buffers are essential for efficiently managing binary data. Their direct handling of raw bytes and versatile features make them perfect for performance-sensitive tasks. Mastering Buffers can significantly boost your ability to work with binary data in your applications.