DEV Community

Neha Sharma
Neha Sharma

Posted on

NodeJS Buffer module

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

Now, when we have large data we use streams Read my blog on streams here.

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

// create

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

Enter fullscreen mode Exit fullscreen mode
// copy

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

buf2.copy(buf1, 2);

Enter fullscreen mode Exit fullscreen mode
// convert to string

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

buf2.copy(buf1, 2);

return buf1.toString();

Enter fullscreen mode Exit fullscreen mode
// Write  

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

buf2.write('hello', 5);

return buf2.toJSON();

Enter fullscreen mode Exit fullscreen mode
// print

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

buf2.copy(buf1, 2);

return buf1.toJSON();

Enter fullscreen mode Exit fullscreen mode
// fill

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

return buf1.toJSON();
Enter fullscreen mode Exit fullscreen mode
// length

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

return buf1.length;

Enter fullscreen mode Exit fullscreen mode

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

Like it? Do follow me on Twitter and Linkedin.

Top comments (0)