Nodejs Buffer Example – Frequently Used Methods

As a product developer and system architect, I tend to use nodejs buffer a lot and in this article, I am listing down the commands and code I use the most.

Node.js buffers provide lots of functions which you may or may not use, check them out here.

This is not a full-fledged tutorial, I am trying to list out the Node.js code I use most wondering it may help out other devs out there too.

Looking for more awesome Node.js tutorials? Check all of them here.

Let’s begin.

What is a buffer in Node js

In simple words, a buffer is a raw binary data and a binary consist of bits that is just a 0 or a 1.

So you can think of a buffer like 101010100001 in raw form.

Buffer representations

As far as i know, you can represent buffer in two forms.

const Buffer = require('buffer').Buffer;

OR

const buf = Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72,
 0x6c, 0x64]);

The first form is raw memory chunk.

console.log(buf);
// outputs <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>

Form 2, decode format (could be utf16le, utf8 etc. more)

console.log(buf.toString('utf16le'));
// outputs '敨汬潷汲'
console.log(buf.toString('utf8'));
// outputs 'hello world'

How to create buffers in Node js

There are multiple ways to create buffers.

You can create using uninitiated Buffer of n octets like this.

const buffer = Buffer.alloc(8);

Nodejs buffer

You can also create a buffer using an array.

This initializes the buffer to the contents of this array. Keep in mind that the contents of the array are integers representing bytes.

const buffer_array = Buffer.from([ 8, 6, 7, 5, 3, 0, 9]);

Nodejs buffer

You can also create a buffer from a string, I use this often.

const buffer_string = Buffer.from("I'm a string!", "utf-8");

Nodejs buffers

The default encoding is utf-8 so you don’t really need to pass it.

Buffer operations

The easiest way to read the buffer and decode those 0’s and 1 is by converting them into a string. Here’s how.

buffer_string.toString();
// read in different encoding
buffer_string.toString('hex');
// read from 0 to 10.
buffer_string.toString('utf-8', 0, 10);

Nodejs buffer

You can convert the buffer into JSON as well. Very useful if you are reading from the database which provides the stream.

const json = JSON.stringify(buffer_string);

JSON.stringify() internally uses toJSON() function of buffer.

You can join multiple buffers together to create a single buffer using concat method.

const joined_buffer = Buffer.concat([buffer_array,buffer_string]);

Nodejs buffers

You can compare two buffers together. It returns 1 if first buffer is greather than second one, -1 if it’s not and 0 if both matches.

buffer_string.compare(buffer_array)

You can copy buffer too. I have never got the use case to use this function. If you do, please let me know.

buffer_string.copy(buffer_array);

You can slice the buffer, just like you do with strings.

const sliced_buffer = buffer_string.slice(0,10);

Probably the most used function, check the length of buffer.

sliced_buffer.length

Conclusion

These are some of the functions which I use a lot. It may help you, if so please let me know.

I will keep updating this article. If you want to add some of the buffer function which you think is very useful, please comment down, I will add them to the article with the credit.

Shahid
Shahid

Founder of Codeforgeek. Technologist. Published Author. Engineer. Content Creator. Teaching Everything I learn!

Articles: 299