Introduction to Buffer
Earlier, the ECMA Script 2015, also known as ES6 JavaScript, had no mechanism to read and manipulate the stream of binary data. The Buffer class was introduced as a part of the Node.js API to make it possible to deal with the stream of data in the context of things, like TCP streams and file system operations. Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory allocations outside the V8 heap. The size of the Buffer is established when it is created and cannot be resized.
In simple words, we can define buffer as, “Buffer is just like a temporary holding spot used for moving the data from one place to another”.
Before the buffer in JavaScript, we read stream data in sequence.
Before the concept of buffer, JavaScript read the stream data in sequence manner - once at a time. That makes it slow. But, in Node.js, comes with concept of buffer that has made the process faster. Let’s see how buffer increases the process speed of Node.js.
In buffer, the data comes down in stream and is hold for a temporary time. When the buffer becomes full, then it sends the data to the process so that it reduces the process time.
Now, we take some example to learn more about the buffer in Node.js.
Create and read Buffer Data.
- var Buf = new Buffer('Pankaj', 'utf8');
- console.log(Buf);
Output
We know that Buffer is an in-built class (module) of Node.js but we don’t need to use the require method because buffer is fundamental concept of Node.js. So, in Node.js, the Buffer class is made as global. In the above example, we create a buffer that's size is equal to the size of “Pankaj” string and we define the “utf8” encoding that means Node.js converts the string data “Pankaj” into binary data using the “utf8” encoding. If we try to read the data of buffer, then it will return the binary data but in hexadecimal representation for easy to read format.
In this example, we convert the binary data into string.
Now, we convert the binary data into JSON format that holds the ASCII value of each character.
Example
- var Buf1 = new Buffer('Pankaj', 'utf8');
- var Buf2 = new Buffer(12);
- var Buf3 = new Buffer([1, 2, 3, 5, 6]);
- var Buf4 = new Buffer(Buf1);
-
- console.log(Buf1);
- console.log(Buf2);
- console.log(Buf3);
- console.log(Buf4)
Output
In the above code snippet, we create 4 buffers. Buf1 occupies the 6 octants (size of string), Buf2 occupies 12 octants, Buf3 occupies 5 octants (size of array) and size of Buf4 is equal to the size of Buf1.
Write Data to Buffer
Using “Write” method of buffer class, we can write the data into a fixed size buffer.
Syntax
Buf.Write(string[,offset][,length][,encoding])
Parameters
Parameter | Description |
String | Data to write in buffer |
Offset | Starting index of buffer to write |
Length | Numbers of bytes to write |
Encoding | Encoding to use, default is ‘utf8’ |
Example:
- var Buf = new Buffer(6);
- var len = Buf.write("pankaj");
- console.log(len);
- console.log(Buf.toString())
Output:
Write method of buffer class returns the numbers of bytes written in buffer.
Example
- var Buf = new Buffer(6);
- var len = Buf.write("pankaj");
- console.log(Buf.toString());
- Buf.write("10");
- console.log("Data Over Write");
- console.log(Buf.toString());
Output
We create a buffer of size 6. If the buffer is full and we write more data, then it will over write the old data.
Example
- var Buf = new Buffer(6);
- var len = Buf.write("pankaj");
- console.log(Buf.toString());
- Buf.write("10",3,2);
- console.log("Data Over Write");
- console.log(Buf.toString());
Output
In this example, we write the data string into buffer at index 3 and length of 2.
Read Data from Buffer
Using “toString” method of buffer class, we can read the data from a buffer.
Syntax:
Buf.toString([,encoding][,start][,end])
Parameter
Parameter | Description |
Encoding | Define mode of encoding, default is “utf8” |
Start | Starting index of buffer to read |
End | End index of buffer to read |
Example
- var Buf = new Buffer(6);
- var len = Buf.write("pankaj");
- console.log(Buf.toString('utf8'));
- console.log(Buf.toString('ascii'));
- console.log(Buf.toString('utf16le'));
- console.log(Buf.toString('ucs2'));
- console.log(Buf.toString('base64'));
- console.log(Buf.toString('binary'));
- console.log(Buf.toString('hex'));
-
- console.log(Buf.toString('utf8',2));
- console.log(Buf.toString('utf8', 2, 3));
Output
Concatenate Buffers
Using “concat” method of buffer class, we can concatenate the list of buffers to a single buffer.
Syntax
Buffer.concat(list[,totalLength])
Parameters
Parameter | Description |
List | List of all buffers to concatenate |
totalLength | Total length of buffers when concatenate |
Example
- var Buf1 = new Buffer("My Name is Pankaj");
- var Buf2 = new Buffer("I live in Alwar");
- var Buf3 = new Buffer("Working as a software developer");
-
- var newBuf = Buffer.concat([Buf1, Buf2, Buf3]);
- console.log(newBuf.toString());
Output
Example
- var Buf = new Buffer("My Name is Pankaj");
- console.log("Buffer Slice from 1 to 4 ="+Buf.slice(1, 4).toString());
- console.log("Buffer Slice from 3 to 6 =" +Buf.slice(3, 6).toString());
- console.log("Length of Buffer Slice ="+Buf.slice(1, 4).length);
Output
Using this example, I tried to explain the slice and length method. Slice method is used to slice or get a portion of buffer and length, I used to get the size of a buffer.
Copy Buffer: Using Copy method of buffer class , data can be copy from one buffer to another buffer.
Syntax: Buf.copy(target_Buffer[,targetStart][,sourceStart][,sourceEnd])
Parameters
Parameter | Description |
Target_Buffer | Buffer where data will copy |
targetStart | Starting index of target buffer, default value is 0 |
sourceStart | Starting index of source buffer, default value is 0 |
sourceEnd | Ending index of source buffer, default value is length of buffer |
Example
- var buf1 = new Buffer("I like Node.js");
- var buf2 = new Buffer(11);
- buf1.copy(buf2, 0, 0, 11);
- console.log(buf2.toString());
Output
ArrayBuffer in JavaScript
In the beginning of this article, I said that JavaScript doesn’t contain any buffer related concept but a new version of V8 engine removes this problem and now, JavaScript contains ArrayBuffer using which we can deal with the binary data. It represents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.
You can get more information about the ArrayBuffer in ES6 documentation.
Example
- var arrbuf = new ArrayBuffer(12);
- var intarr = new Int32Array(arrbuf);
- intarr[0] = 10;
- intarr[1] = 23;
- intarr[2] = 45;
- console.log(intarr.toString());
Output
I hope you liked this article. If you have any doubts, then write in the comment section.