Node.js – Buffer Length

Node.js – Buffer Length – To get Buffer length in Node.js, use the methodBuffer.length .

Syntax

Buffer.length

Buffer.length returns the amount of memory allocated to the buffer in bytes.

length property of Buffer class is immutable.

Example – Buffer created from a string

Following is an example to the usage of Buffer.length method :

When buffer is created from the supplied string, it allocates those number of bytes as that of in the string, to the buffer.

Example – Buffer created using alloc() method

In the following example, buffer is allocated a specific number of bytes, and then data(not the size of buffer) is written to the buffer. We shall see what Buffer.length returns for this buffer.

It does not matter how many bytes you have overwritten from the allocated memory of buffer, but Buffer.length always returns the number of bytes allocated to the Buffer.

Conclusion :

In this Node.js Tutorial, we have learnt to find the length of Buffer in Node.js.