Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Node Buffer?
3.
Why do we need Buffers?
4.
Creating a Buffer in NodeJS
5.
Using Buffers
5.1.
Getting the Length of a Buffer
5.2.
Accessing the Content of a Buffer
5.3.
Modifying the Contents of a Buffer
5.4.
Copying the Content of a Buffer
6.
Other Buffer Class Methods
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Node Buffer

Author Gunjeev Singh
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

NodeJS is a Javascript runtime environment, allowing us to execute and run javascript code outside browsers. An essential functionality and feature that NodeJS offers us is the concept of Node Buffers. This blog will look into Buffers in NodeJS, what they are, why we need them, and how to use them efficiently. A prerequisite for understanding the contents of this blog would be having a basic understanding of What NodeJS is and why we use it.

 

Before we start learning about the concept of Node Buffers, we should answer one fundamental question - Why do we need to understand memory management with Node? The answer to this is simple. Memory is the most scarce resource for any developer, and knowing how to store, access, and manipulate data efficiently is a must in today's world.

What is a Node Buffer?

To put it in straightforward terms, a Node 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. To know more about the intricacies of the buffer class, you can refer to the documentation here.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Why do we need 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. 

Creating 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 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])

 

  • Using the alloc() and 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)


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)

 

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]) 

 

Output :

 

72
101
108

 

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

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!') 

 

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()) 

 

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) 

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

1. What would be the output if we print a buffer that has not been initialized?
If a buffer has not been initialized, it would contain random raw binary data. Using the console.log() the toString() of this buffer, we would see UTF-8 values of whatever random raw binary data is stored in the buffer by default. 

 

2. What does slicing a buffer mean?
A slice of a buffer can be defined as a visualization of a specified part of a buffer. It is important to note that this is not the same as a copy. The original buffer is still the parent identity. If the initial buffer changes, the slice would change automatically. We use the subArray() function to slice a buffer and the raw binary data it holds.

Key Takeaways

In this blog, we learned the concept of buffers in Node. We now know that buffers are so important because they help store raw binary data. Raw binary data is nothing but raw bytes or chunks of main memory. Gaining access to raw main memory helps us get complete control over a system's resources.

 

Now that you know Node Buffers, you can start your MERN stack journey here! If you want to know more about NodeJs, check out the blog What is Node.js? Where, When & How To Use It?

Previous article
Streams and Buffer in Node.Js
Next article
Stream Pipes in NodeJs
Live masterclass