Buffer buffer in Node.js (summary of concepts and common methods)

Posted by Pyro on Thu, 09 Sep 2021 18:19:30 +0200

Buffer Definition

Javascript is good at strings, but not binary data (such as TCP streams). (Binary data must be used when working with streams like TCP or files)Thus, in Node.js, a Buffer class is defined to create a cache dedicated to binary data, where the data is an integer value between 0 and 255 (that is, one byte of data)

Generally speaking, we usually transfer data to process it, read it, or process it based on it. However, there is a problem with the amount of data in each transmission. So when the data arrives faster than the time the data is supposed to take, we have to wait to process the data.

Or, if the processing time is faster than the arrival time, and only a small portion of the data arrives at that time, the small portion of the data needs to wait until the rest of the data is filled and then sent to the past for uniform processing.

This waiting area is buffer!

The Buffer object is an interface for Node to process binary data. It is a global object that Node provides natively and can be used directly without requiring ('buffer') to introduce a buffer.

Buffer object

Buffer objects, also known as buffer objects, have a constructor that creates objects for which the v8 engine allocates a block of memory

Create a buffer object

(1) Using the new operator

//Create an empty buffer of size 8
var bytes=new Buffer(8)
for(var i=0;i<bytes.length;i++){
//<Buffer 00 01 02 03 04 05 06 07>

(2) Using the from function

const buf1=Buffer.from([1,2,3])
//<Buffer 01 02 03>

(3) Use the alloc function

For example, create a 10-length buffer and fill it with 0

const buf2=Buffer.alloc(10)
//<Buffer 00 00 00 00 00 00 00 00 00 00>

Copy of cache

Follow up example

var more=new Buffer(4)
//<Buffer 04 05 06 07>

Buffer and character encoding

Buffer instances are typically used to represent sequences of encoded characters, such as UTF-8 or hexadecimal-encoded data. By using explicit character encoding, you can convert Buffer instances to and from regular JavaScript strings.

const buf=Buffer.from('Hello World','ascii')
//<Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>

const buf2=Buffer.from('tést')
//<Buffer 74 c3 a9 73 74>


Buffer to JSON

JSON: JavaScript Object Notation object, a common format for front-end and back-end data exchange, stores data in a key-value way. It is widely used because of its small size, easy creation and simple parsing.
Applications can use structured information as long as it needs to be exchanged as text or between villages.

const buf=Buffer.from([0x1,0x2,0x3,0x4,0x5])
var json=JSON.stringify(buf)


Buffer write data


buf.write(string[, offset[, length]][, encoding])
  • String - The string that is written to the buffer.
  • offset - The index value that the buffer begins to write, defaulting to 0.
  • Length - The number of bytes written, defaulting to buffer.length
  • Encoding - The encoding used. Default is'utf8'.

For example:

var buf=Buffer.alloc(256)
let len=buf.write('Hello')
console.log(`Bytes Written:${len}`)
//Bytes written: 5

Note: If there is not enough buffer space, only part of the string will be written.

Read Buffer Data


buf.toString([encoding[, start[, end]]])

For example:

var buf8=Buffer.alloc(256)
let len=buf8.write('\u00bd+\u00bc=\u00be',0);   //The return value of the write function is the number of bytes written, starting at position 0

//8 characters: _+_=_

Topics: node.js