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.
Table of Contents
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.
OR
0x6c, 0x64]);
The first form is raw memory chunk.
// outputs
Form 2, decode format (could be utf16le, utf8 etc. more)
// 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.
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.
You can also create a buffer from a string, I use this often.
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.
// read in different encoding
buffer_string.toString('hex');
// read from 0 to 10.
buffer_string.toString('utf-8', 0, 10);
You can convert the buffer into JSON as well. Very useful if you are reading from the database which provides the stream.
JSON.stringify() internally uses toJSON() function of buffer.
You can join multiple buffers together to create a single buffer using concat method.
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.
You can copy buffer too. I have never got the use case to use this function. If you do, please let me know.
You can slice the buffer, just like you do with strings.
Probably the most used function, check the length of buffer.
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.
Nice article. Could you also provide some practical use of this in enterprise applications. Thanks
A common use case is character encoding, usually base64. If you accept input in your app as a buffer, then you can easily say buffer.toString(‘base64’) so ‘hello buffer’ would be ‘aGVsbG8gYnVmZmVy’ instead. You can store or transmit this encoded value and then receive in another buffer, const val = Buffer.from(‘aGVsbG8gYnVmZmVy’, ‘base64’); Thereafter you can either say val.toString(); which prints ‘hello buffer’ or you can display it in any desired format.
Nice article at first place.
I have a query related to Buffer class. I want to use Buffer object in client-side code in typescript. but at run time I am getting error “Buffer not defined” when creating buffer (new Buffer(32)).
I installed buffer using “npm i buffer”. Please help me sort this out.