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.
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");
// copy
var buf1 = Buffer.from('abcdefghijkl');
var buf2 = Buffer.from('HELLO');
buf2.copy(buf1, 2);
// convert to string
var buf1 = Buffer.from('abcdefghijkl');
var buf2 = Buffer.from('HELLO');
buf2.copy(buf1, 2);
return buf1.toString();
// Write
var buf1 = Buffer.from('abcdefghijkl');
buf2.write('hello', 5);
return buf2.toJSON();
// print
var buf1 = Buffer.from('abcdefghijkl');
var buf2 = Buffer.from('HELLO');
buf2.copy(buf1, 2);
return buf1.toJSON();
// fill
var buf1 = Buffer.alloc(5);
buf1.fill('n');
return buf1.toJSON();
// length
var buf1 = Buffer.from('abcdefghijkl');
return buf1.length;
summary
Buffers work closely with
fs
,streams
, andbinary data
buffer is a temp place to store the data
Computers understand only binary data (0s, and 1s)
Buffer helps in manipulating and reading of the stream of binary data
You can create your own buffer
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.
Top comments (0)