JavaScript

How to Copy, Compare, and Concatenate a Buffer in Node.js?

Buffer is the collection of binary data that is stored temporarily in a particular memory. Its main purpose is to manage and transfer the stored binary data from one place to another. In Node.js, the user can access that particular memory with the help of the built-in “Buffer” module.

Buffer is more similar to the JavaScript “array” but with one difference i.e. its size can not be changed once it has been set. The common features of this module are writing, reading, comparing, copying, converting, and many others. All of the specified operations can be performed using its built-in methods and properties.

This blog will provide a brief description of copying, comparing, and concatenating a buffer in Node.js.

Let’s start with the “copy” operation.

How to Copy a Buffer in Node.js?

The “Buffer.copy()” method allows the users to copy one buffer object to another. This method returns a newly updated buffer as a result. The working of this method depends on its following syntax:

buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])

According to the above syntax, the “copy()” method tasks following parameters:

  • targetBuffer: It specifies the Buffer object whose content needs to be copied into another buffer object.
  • targetStart: It denotes the position where the copy to operation will start. Its default value is “0”.
  • sourceStart: It denotes the position where the copy operation will start. Its default value is “0”.
  • sourceEnd: It represents the ending position where the copy operation will stop. Its default value is the “end of a buffer”.

The following code block shows the practical implementation of the “copy()” method:

var buf1 = Buffer.from('123');
var buf2 = Buffer.alloc(3);
buf1.copy(buf2);
console.log("Content of Buffer2 is: " + buf2.toString());

In the above code lines:

  • The “from()” method creates a buffer object with the specified integers.
  • The “alloc()” method constructs a buffer object of an allocated size.
  • The “copy()” method copies the pre-existing content of the first buffer into the second buffer object.
  • The “toString()” method decodes the copied content into string format(human-readable) and then displays it in the console using the “console.log()” method.

Output
Execute the given command to initiate the “.js” file:

node app.js

It can be seen that the content of one buffer has been copied into the second buffer:

How to Compare a Buffer in Node.js?

To compare a buffer in Node.js, use the built-in “Buffer.compare()” method. This method compares two buffers and returns a numeric value as its standard output that shows the defined differences. These numeric values are listed below along with their defined differences:

  • 0: Buffers are equal.
  • 1: Buffer 1 is greater than Buffer 2.
  • -1: Buffer 1 is lower than Buffer 2.

Syntax

buf.compare(otherBuffer);

The syntax of the “compare()” method only supports one essential argument which is “otherBuffer”.

Let’s use the above-defined syntax practically:

var buf1 = Buffer.from('Linux');
var buf2 = Buffer.from('Linux');
var x = Buffer.compare(buf1, buf2);
console.log(x);

var buf1 = Buffer.from('L');
var buf2 = Buffer.from('H');
var x = Buffer.compare(buf1, buf2);
console.log(x);

var buf1 = Buffer.from('H');
var buf2 = Buffer.from('L');
var x = Buffer.compare(buf1, buf2);
console.log(x);

In the above lines of code:

  • The “from()” methods create multiple buffer objects with the specified string.
  • The “compare()” method compares the specified buffer objects that passed as its argument.
  • The “console.log()” method displays the result of the “compare()” method on the console.

Output
Run the “.js” file using below command:

node app.js

The numerical values displayed as a resultant output clearly show the defined difference:

Now, move on to the concatenation of buffers in Node.js.

How to Concatenate Buffers in Node.js?

The “concat()” method helps to join more than one buffer. This method concatenates all the targeted buffers in an array into one buffer object by merging their contents.

Syntax

Buffer.concat(arr, length])

The above syntax works on the following two parameters:

  • arr: It specifies the array of buffers that users want to concatenate.
  • length: It denotes the length of the concatenated buffer.

The following code snippet practically concatenates the given buffers by using the defined “concat()” method:

var buf1 = Buffer.from('\nLinuxhint\n');
var buf2 = Buffer.from('Website');
var buf3 = Buffer.concat([buf1,buf2]);
console.log("Content of Buffer3 is: " + buf3.toString());

In the above code snippet:

  • The “from()” methods create two buffer objects with the specified string respectively.
  • The “concat()” method joints the specified buffer in an array into a buffer object.
  • The “toString()” method decodes the content of the concatenated buffer into string format(human-readable) and then displays it in the console using the “console.log()” method.

Output
Now, run the below-stated command to execute the “.js” file:

node app.js

The output displays a new buffer object that is created in the concatenation result of the specified buffers:

We have covered how to copy, compare, and concatenate the buffer in Nodejs.

Conclusion

To copy the buffer in Nodejs, use the “Buffer.copy()” and for comparison purposes apply the “Buffer.compare()” method. In addition, to concatenate buffers, utilize the “Buffer.concat()” method. All these methods are pre-defined, simple, as well as easy to use. This post has provided a brief description of copying, comparing, and concatenating a buffer in Node.js.

About the author

Areej Nadeem

I am a technical author holding a Bachelor’s degree in Computer Science. I am passionate about writing and learning new technologies and sharing my knowledge with the rest of the world.