Overview
This article is Part 6 of the series Working With NodeJS a series of articles to help simplify learning Node.js. In this article I will cover Buffers.
Introduction
Handling binary data in server-side programming is essential. Binary data is much faster and more efficient in terms of memory storage and processing then plain text. In Node.js, binary data is handled with a Buffer. Buffers act somewhat like arrays of integers, but aren't resizable and have a bunch of methods specifically for binary data.
The Buffers Instance
The Buffer constructor is a global object, so there's no need to require any core module in order to use it. There are a few ways to create a Buffer:
Instance 1
const buffer = Buffer.alloc(10);
The above would allocate a buffer of 10 bytes. By default the Buffer.alloc
function produces a zero-filled buffer. Using Buffer.alloc
is the safe way to allocate buffers.
Instance 2
const buffer = Buffer.allocUnsafe(10);
The allocUnsafe
method poses potential security risk, but has it's advantages. Unallocated memory is only unlinked, it isn't wiped. This means that unless the buffer is overwritten (e.g. zero-filled) then an allocated buffer might contain fragments of previously deleted data. However, it is much faster then its counterpart alloc
method. If we need to allocate memory for a buffer, it's strongly recommended to use Buffer.alloc
instead of Buffer.allocUnsafe
.
Instance 3
const buffer = Buffer.from([1, 2, 3]);
This initializes a buffer from an array.
Instance 4
const buffer = Buffer.from("I'm a string!", "utf-8");
This initializes a buffer from
a string with encoding specified by the second argument in this case utf-8.
Working With Buffers
Working with buffers are pretty straight forward. When a buffer is created using the alloc
method, it zero-fills the buffer with the number of bytes that was passed to the alloc
method.
const buffer = Buffer.alloc(10);
console.log(buffer);
// <Buffer 00 00 00 00 00 00 00 00 00 00>
In the above code snippet, the buffer is zero-filled with a fixed length of ten bytes that can be used for the buffer. To write data to the buffer, we can use the write
method.
const buffer = Buffer.alloc(10);
buffer.write("Some data");
console.log(buffer);
// <Buffer 53 6f 6d 65 20 64 61 74 61 00>
Now, the buffer is filled with Some data just in the form of bytes. Each byte in the buffer represents a character in the string Some data. Notice the 00
at the end of the byte sequence. If you count the characters in Some data, including the space, it equates to nine. The 00
represents unused memory that was allocated for the buffer. If I add an additional character to the string Some data the buffer will fill its allocated memory.
const buffer = Buffer.alloc(10);
buffer.write("Some data2");
console.log(buffer);
// <Buffer 53 6f 6d 65 20 64 61 74 61 32>
The number of bytes written to the buffer is a value returned by the write
method. If we attempt to append more characters to the string then what was allocated, the data will simply not be included in the buffer.
'use strict';
const buffer = Buffer.alloc(10);
const byteCount = buffer.write("Some data2 Adding more data");
console.log(byteCount); // 10
If we wanted to read the buffer, we can call the toString
method on the buffer object.
'use strict';
const buffer = Buffer.alloc(10);
const byteCount = buffer.write("Some data2");
console.log(buffer.toString()); // Some data2
Before ending this article, you should know there are few security concerns with the buffer object. However, the examples shown in this article avoid the security issues highlighted on the Node.js Official Docs.
If you would like to learn more about the Buffer class and some of its methods, Head over to the Node.js Official Documentation. If you found this article helpful I ask that you subscribe to the series as more content is on the way. Take care.
Top comments (0)