So far we have covered
buffer is related to the
streams. Today we will explore an interesting topic -
To understand buffers we should know how internals working of computer and content handling.
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.
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.
Buffer is a temporary place to store, and translate the data into binary before passing it to the next destination (CPU).
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.
// 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;
Buffers work closely with
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.