Table of contents
1.
Introduction
2.
What is a Buffer in Node.js?
3.
Why do we need Node.js Buffers?
4.
Methods of Buffer 
5.
How to Create a Buffer in NodeJS?
5.1.
Using the Buffer.from() method:
5.2.
Using the Buffer.alloc() and Buffer.allocUnsafe() methods
6.
Using Buffers
6.1.
Getting the Length of a Buffer
6.2.
Accessing the Content of a Buffer
6.3.
Modifying the Contents of a Buffer
6.4.
Copying the Content of a Buffer
7.
Example of Node.js Buffers
7.1.
Example: Creating and Manipulating a Buffer in Node.js
8.
Other Buffer Class Methods
9.
Frequently Asked Questions
9.1.
How are Buffers different from arrays?
9.2.
When should I use a Buffer in a Node.js application?
9.3.
Why are Buffers used in Node.js instead of strings?
9.4.
Is Buffer memory managed automatically in Node.js?
10.
Conclusion
Last Updated: Oct 15, 2024
Easy

Buffer in Node.js

Author Gunjeev Singh
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Node.js, a buffer is a low-level data structure representing a fixed-size chunk of memory. It is used to store and manipulate binary data, such as file contents or network packets. Buffers provide a way to handle raw bytes efficiently. They are mutable, and resizable, and offer methods for reading, writing, and manipulating data. Buffers are essential for working with streams and handling binary data in Node.js. In this article, we will discuss this in detail like what it is, why we even need a Node buffer, how to create one, different methods to use it, etc.

Buffer in Node.js

What is a Buffer in Node.js?

To put it in straightforward terms, a Node.js Buffer can be defined as an area of memory. The concept of Buffers is most predominant in core languages like C++, Java, etc., which directly deal with memory. Most Javascript developers are less familiar with this term. 

 

A Node Buffer is a fixed chunk of memory allocated on definition outside the core Javascript V8 engine.

 

A Node Buffer can be thought of as an array, but each unit stores raw binary data. The fact that Node Buffer stores raw binary data make it an essential feature as it helps in manipulating memory directly.

 

Buffers are implemented using the Buffer Class in Node. It is a global class that is accessed in an application without importing the buffer module. 

Why do we need Node.js Buffers?

It is important to note that traditionally the Javascript ecosystem dealt with data in strings, not raw binary data. Buffers were introduced in NodeJS to add this missing functionality in vanilla Javascript. Pure Javascript is easy to use and compatible with Unicode, but it is not very friendly with raw binary data. We often need to handle octet streams or the file system, which is why Buffers are required. We need buffers because they provide instances that can deal with memory allocation outside the V8 engine heap. 

Methods of Buffer 

Let's see the different Node.js Buffer methods:

MethodDescription
alloc(size[, fill])Allocates a new buffer of the specified size. Optionally, fills the buffer with a specific value.
from(array)Creates a new buffer from an array of octets (bytes).
from(string[, encoding])Creates a new buffer from a string with an optional character encoding (default: UTF-8).
isBuffer(obj)Checks if an object is a buffer.
concat(list[, totalLength])Concatenates an array of buffers into a single buffer.
compare(buf1, buf2)Compares two buffers lexicographically.
copy(target[, targetStart[, sourceStart[, sourceEnd]]])Copies data from one buffer to another.
equals(otherBuffer)Checks if two buffers are equal.
fill(value[, offset[, end]])Fills a buffer with a specified value.
includes(value[, byteOffset][, encoding])Checks if a buffer includes a specific value.
indexOf(value[, byteOffset][, encoding])Returns the first index at which a specified value is found in a buffer.
lastIndexOf(value[, byteOffset][, encoding])Returns the last index at which a specified value is found in a buffer.
lengthReturns the length of a buffer in bytes.
slice([start[, end]])Returns a new buffer that references the same memory as the original, but offset and cropped by the start and end indices.
subarray([start[, end]])Returns a new buffer that references the same memory as the original, but offset and cropped by the start and end indices.
toString([encoding[, start[, end]]])Decodes a buffer to a string with an optional character encoding, start, and end positions.
write(string[, offset[, length]][, encoding])Writes a string to a buffer at the specified offset, with an optional length and character encoding.
writeDoubleBE(value[, offset])Writes a 64-bit double in big-endian format to a buffer at the specified offset.
writeDoubleLE(value[, offset])Writes a 64-bit double in little-endian format to a buffer at the specified offset.
writeFloatBE(value[, offset])Writes a 32-bit float in big-endian format to a buffer at the specified offset.
writeFloatLE(value[, offset])Writes a 32-bit float in little-endian format to a buffer at the specified offset.

How to Create a Buffer in NodeJS?

The buffer class in Node offers many methods to create a buffer. We can use the Buffer.from()Buffer.alloc(), and the Buffer.allocUnsafe() methods of the Buffer class to make Buffers in NodeJS. 

Using the Buffer.from() method:

We can use the Buffer.from() method to create a Buffer like:

 

const example = Buffer.from('Hello World!')

 

This method has the following variations:

 

Buffer.from(array)
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(buffer)
Buffer.from(string[, encoding])
You can also try this code with Online Javascript Compiler
Run Code

 

Using the Buffer.alloc() and Buffer.allocUnsafe() methods

In these two methods, we simply pass the size (in bytes) of the buffer that we require:
 

// Creates a buffer of 1Kb
Const example = Buffer.alloc(1024) 

// Creates a buffer of 1 Kb
Const example = Buffer.allocUnsafe(1024)
You can also try this code with Online Javascript Compiler
Run Code


Though both these methods create a buffer specified by its size in bytes, there is an essential difference between the two. alloc() initializes the buffer it creates with zeroes, while allocUnsafe() creates a buffer without initializing it. Though this makes allocUnsafe() a lot faster, we should also use it with care because the raw binary data that the buffer would occupy might be sensitive.

Using Buffers

In this section, we will learn how to access the contents of a buffer, modify them, copy them, and some more functionalities. 

Getting the Length of a Buffer

As we know, a buffer is a fixed-size memory allocation that the buffer cannot resize. This means that a buffer must have a length. We can use the length property to know the length of a buffer.

 

const example = Buffer.from('Hello World!')
console.log(example.length)
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

12

Accessing the Content of a Buffer

As we have discussed above, a buffer is nothing but an array of raw binary data. Hence, we can access its contents like that of an array. 

 

const example = Buffer.from('Hello World!')

// Accessing the first element
console.log(example[0])

// Accessing the second element
console.log(example[1])

// Accessing the third element
console.log(example[2]) 
You can also try this code with Online Javascript Compiler
Run Code

 

Output :

 

72
101
108
You can also try this code with Online Javascript Compiler
Run Code

 

We get this output because these are the UTF-8 values of the characters 'H', 'e,' and 'l,' respectively. 

 

We can also print the entire buffer by converting it into a string like:

 

const.log(example.toString()); // Using the toString() function
You can also try this code with Online Javascript Compiler
Run Code

Modifying the Contents of a Buffer

We can write a whole string of data on a buffer using the write() method. 

 

// Creates a buffer of 12 bytes and initializes it with zeroes
const example = Buffer.alloc(12) 

// Writes Hello World! on those 12 bytes of allocated memory
example.write('Hello World!') 
You can also try this code with Online Javascript Compiler
Run Code

 

Just like we can access the contents of a buffer like that of an array, we can also write to specific indices of the buffer, just like an array. 

 

const example = Buffer.from('Hello World!')

// Letter o in UTF-8
example[0] = 111

// Prints the contents of the buffer
console.log(example.toString()) 
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

 

oello World!

Copying the Content of a Buffer

We can copy a buffer using the predefined set() method. 

 

const example = Buffer.from('Hello World!')

// Allocate 12 bytes
const examplecopy = Buffer.alloc(12)

// Changes the contents of examplecopy to Hello World! 
examplecopy.set(example) 
You can also try this code with Online Javascript Compiler
Run Code

 

Output :

Hello World!

Example of Node.js Buffers

Node.js Buffers are used to handle binary data directly in memory, which can be more efficient for certain types of operations, like handling files or network data. Now, let's see a simple example of creating and using a Buffer in Node.js : 

Example: Creating and Manipulating a Buffer in Node.js

 

// Import the buffer module
const Buffer = require('buffer').Buffer;
// Create a buffer from a string
const buf = Buffer.from('Hello World');
// Log the buffer contents as a string
console.log(buf.toString());
// Create another buffer with a specified size
const bufSize = Buffer.alloc(12); // Allocates a buffer of 12 bytes
// Copy the first buffer to the new buffer
buf.copy(bufSize);
// Log the new buffer contents as a string
console.log(bufSize.toString());

 

Output:

Hello World
Hello World

Other Buffer Class Methods

Some other commonly used Buffer class methods are as follows:

 

Method 

Usage

values()Returns an array of values in a Buffer Object
slice()Slices a Buffer object into new Buffer objects with given starting and ending points.
toJSON()Returns a JSON version of a buffer object.
isBuffer()Checks if an object is a Buffer
compare()Compare two buffer objects.

Frequently Asked Questions

How are Buffers different from arrays?

Buffers directly manipulate binary data in memory, while arrays in JavaScript typically handle elements that are higher-level objects or primitives.

When should I use a Buffer in a Node.js application?

We can use a Buffer in Node.js when we are dealing with raw binary data, like file system operations, network data handling, or interfacing with certain binary databases.

Why are Buffers used in Node.js instead of strings?

Buffers are used instead of strings in Node.js to handle binary data efficiently, which is necessary for tasks that involve reading and writing to files or sockets directly.

Is Buffer memory managed automatically in Node.js?

Yes, Buffer memory in Node.js is managed automatically by the V8 JavaScript engine's garbage collector, similar to other JavaScript objects.
 

Conclusion

In this article, we have learned about Node.js Buffers and how they differ from regular arrays. Buffers are crucial for handling binary data directly within an application, making them essential for tasks involving files, networks, and binary databases. We explained when and why to use Buffers, highlighting their efficient data management capabilities and automatic memory handling by Node.js's garbage collection system. 

Live masterclass