English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer & URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File (FS)

NodeJS Other

Node.js Buffer (Buffer) – Create, write, and read

Node.js Buffer

Node.js Buffer– The Node.js Buffer is a class that helps to handle and use eight-bit byte streams. It is often encountered in handling TCP data streams and file system operations, where eight-bit byte streams are commonly used.

The original memory allocated to the buffer in Node.js V8Outside of heap memory.

In this tutorial, we will learn how to

  • Create buffer

  • Write data to the buffer

  • Read data from the buffer.

Node.js – Create Buffer

There are many ways to create a buffer in Node.js. We will study them one by one.

Buffer of specified length

To create a buffer of a specified length, use the Buffer.allocUnsafe(int) method.

Syntax

Buffer.allocUnsafe(bufferLength);
const buf1 = Buffer.allocUnsafe(10);

bufferLength is an integer that specifies the length of the buffer to be created.

The created buffer is not initialized, which means it can contain garbage values. You can use the fill() or write() method to overwrite garbage values.

Zero-filled buffer of specified length

To create a zero-filled buffer of a specified length, use the Buffer.alloc(int) method.
Syntax

Buffer.alloc(bufferLength);
const buf1 = Buffer.alloc(10);

 bufferLength is an integer that specifies the length of the buffer to be created. The buffer contains all memory locations filled with zeros.

Buffer.alloc() is slower than Buffer.allocUnsafe().

Buffer of specified length, filled with specified value

To create a buffer of specified length and filled with specified values, use the Buffer.alloc(int, int) method.
Syntax
Buffer.alloc(bufferLength, value);
 

  const buf1 = Buffer.allocUnsafe(10, 3);

 bufferLength is an integer that specifies the length of the buffer to be created. The buffer contains all memory locations filled with the value.

Node.js – Writing to Buffer

The Buffer class has many methods that can write different formats of data into a buffer. In this section, we will learn to write a string into a buffer.
To write a string to a buffer, use the Buffer.write method.
Syntax

Buffer.write(string[, offset[, length]][, encoding]);
  const buf1 = Buffer.allocUnsafe(100);
 
  const len = buf1.write('welcomeuser',2,5,'utf8');

A string starting from offset, with the number of characters provided by the length, will be written to the buffer in the specified encoding format.

The write() method returns the number of bytes written to the buffer.

Node.js – Reading from Buffer

To read bytes from a buffer, use the Buffer.values method.
Syntax

Buffer.values();

Create and return buf1An iterator of buffer values (bytes). When using Buffer in for..of statements, this function is automatically called.

  const buf1 = Buffer.allocUnsafe(11);
 
  const len = buf1.write('welcomeuser');
 
  for(const byt of buf1.values()]{
    console.log(byt);
  }

Output Result

$ node read-buffer.js  
119
101
108
99
111
109
101
117
115
101
114

Conclusion:

In this Node.js tutorial – Node.js BufferWe learned how to create, write, and read buffers in Node.js. In the next tutorial, we will learn some interesting operations related to buffers.