Node.js Buffers

Node.js Buffers

When working with raw binary data in Node.js, the Buffer class plays a key role. Unlike arrays, Buffers represent raw memory allocations outside the V8 heap, making them essential for handling binary data in operations such as file system interactions and TCP streams.

Introduction to Applied AI:–Click Here

JavaScript on its own does not handle binary data efficiently, which is why the Buffer class was introduced. The good part is that Buffer is a global class, so you can use it directly without importing the buffer module.

Creating Buffers in Node.js

There are several ways to create a Buffer, but the following three are the most common:

Data Science Tutorial:-Click Here

  1. Creating an uninitialized buffer

var buf = new Buffer(10);

This creates a buffer of 10 octets.

  1. Creating a buffer from an array

var buf = new Buffer([10, 20, 30, 40, 50]);

This initializes a buffer with the values provided in the array.

Download New Real Time Projects :–Click here

  1. Creating a buffer from a string

var buf = new Buffer("Simply Easy Learning", "utf-8");

This creates a buffer from a given string, with an optional encoding (default is UTF-8).

Writing to Buffers

To write data into a buffer, Node.js provides the write() method.

Syntax:

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

  • string: Data to be written into the buffer.
  • offset: Position to start writing. Default is 0.
  • length: Number of bytes to write. Default is the buffer’s length.
  • encoding: Encoding type. Default is utf8.

Return Value:
Returns the number of octets written. If the buffer doesn’t have enough space, only part of the string will be written.

Machine Learning Tutorial:–Click Here

Example:
File: main.js

buf = new Buffer(256);  
len = buf.write("Simply Easy Learning");  
console.log("Octets written : " + len);

Run the file using:

node main.js

Output:

Octets written : 20

Reading from Buffers

To read data from a buffer, we use the toString() method.

Complete Advance AI topics:- CLICK HERE

Syntax:

buf.toString([encoding][, start][, end])

  • encoding: Character encoding (default is utf8).
  • start: Starting index (default is 0).
  • end: Ending index (default is the complete buffer).

Return Value:
Returns a decoded string from the buffer data.

Example:
File: main.js

buf = new Buffer(26);  
for (var i = 0; i < 26; i++) {  
  buf[i] = i + 97;  
}  

console.log(buf.toString('ascii'));        // abcdefghijklmnopqrstuvwxyz  
console.log(buf.toString('ascii', 0, 5));  // abcde  
console.log(buf.toString('utf8', 0, 5));   // abcde  
console.log(buf.toString(undefined, 0, 5));// defaults to utf8 -> abcde

Run the file using:

node main.js

Output:

abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde

Would you like me to also add a section on updating/deprecations (like using Buffer.alloc and Buffer.from instead of new Buffer()) since new Buffer() is deprecated in modern Node.js? That will make the article more up-to-date and professional for UpdateGadh.

Deep Learning Tutorial:– Click Here
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here


node js buffer example
node js buffers w3schools
streams and buffers in node js
how to create buffer in node js
buffer – npm
nodejs buffer to string
nodejs buffer to base64
buffer from base64

Share this content:

Post Comment