NodeJS Buffer module
Tags: webdev, node, programming, javascript
Published: 2022-12-05 22:10:11

So far we have covered `fs`, and `streams`. `buffer` is related to the `fs` and `streams`.  Today we will explore an interesting topic - `buffer`

## History

Computers understand only binary data (0,1). They do not understand any other data-type. However, JavaScript deals with strings but it is for the web. When it comes to the non-web then instead of strings we work with binary data. 

### Why we are talking about this?

To understand buffers we should know how internals working of computer and content handling.


## 1. What is buffer

Buffer is a mechanism of reading and manipulating streams of binary data. It is a temporary space in your computer system where the data is store before it gets manipulated in binary data( 0s and 1s) and send ahead for the task. 

#### What is Binary data? 

Binary data is collection of 0s, and 1s , this is what our computers understand. 

Everything first convert to the numbers and then binary data.

Imagine that computer is getting subtitle of the task it is supposed to do. Subtitle should be binary as this is the only language computer understands.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7ixqp4m992dgu1bu51j.png)

Now, when we have large data we use streams [Read my blog on streams here](https://dev.to/hellonehha/4-nodejs-module-streams-io2).

In short, streams sends the small chunk of data from one place to another. The chunk of the data will be saved in the buffer and wait for the system to be ready to process the data. 

When the data is moving from file to destination if there the flow of the data is more than it can be consumed. Data will wait in buffer.


## 2. Why buffer

Buffer is a temporary place to store, and translate the data into binary before passing it to the next destination (CPU).


## 3. NodeJS and Buffer

We can create our own Buffer by use of the Buffer in NodeJS.
NodeJS has native module which we can use to create the buffer. This helps the programmers to create an extra temp place and have the control over the content.

## Code Example

```javascript
// create

const buf1 = Buffer.alloc(10);
const buf2 = Buffer.from("hello buffer");

```

```javascript
// copy

var buf1 = Buffer.from('abcdefghijkl');
var buf2 = Buffer.from('HELLO');

buf2.copy(buf1, 2);

```

```javascript
// convert to string

var buf1 = Buffer.from('abcdefghijkl');
var buf2 = Buffer.from('HELLO');

buf2.copy(buf1, 2);

return buf1.toString();

```

```javascript
// Write  

var buf1 = Buffer.from('abcdefghijkl');

buf2.write('hello', 5);

return buf2.toJSON();

```


```javascript
// print

var buf1 = Buffer.from('abcdefghijkl');
var buf2 = Buffer.from('HELLO');

buf2.copy(buf1, 2);

return buf1.toJSON();

```
```javascript
// fill

var buf1 = Buffer.alloc(5);
buf1.fill('n');

return buf1.toJSON();
```

```javascript
// length

var buf1 = Buffer.from('abcdefghijkl');

return buf1.length;

```


### summary

1. Buffers work closely with `fs`, `streams`, and `binary data`

2. buffer is a temp place to store the data

3. Computers understand only binary data (0s, and 1s) 

4. Buffer helps in manipulating and reading of the stream of binary data 

5. You can create your own buffer

6. Buffer helps in improving the performance of the application. Eg: by using backpressuring in streams we can pause() and resume() read/write operation and we can take advantage of the buffer.


### Resources

[NodeJS Documentation](https://nodejs.org/api/stream.html)

Like it? Do follow me on [Twitter](https://twitter.com/hellonehha) and [Linkedin](https://www.linkedin.com/in/nehha/).