Node.js - Buffer.compare() Method



The NodeJS Buffer.compare() method helps to deal with the comparison of two buffer objects and returns a value that states the difference in comparison. It returns 0, 1 and -1, where 0 means the given two buffers are equal, 1 means the first one is greater and -1 means the first one is less than the second one.

The comparison is done based on the bytes sequence in each buffer.

Syntax

Following is the syntax of the NodeJS compare() method −

Buffer.compare(targetBuffer, targetStart,targetEnd, sourceStart, sourceEnd)

Parameters

The Buffer.compare() method takes in five parameters as shown in the syntax above. The details of each param is mentioned below −

  • target Buffer −This param takes in the buffer object that will be used to compare with other buffers given.

  • targetStart −This param takes in the offset value that falls in the range of targetBuffer and also the start point to start comparison. The default value is 0.

  • targetEnd −This param takes in the offset value that falls in the range of targetBuffer and also the end point to end comparison. The default value is targetBuffer.length.

  • sourceStart −The index from which the comparison will start and its default value is 0.

  • sourceEnd −The end index at which the comparison will stop.The default value is length of the buffer.

Please note targetStart, targetEnd, sourceStart and sourceEnd are optional params in Buffer.compare().

Return value

The Buffer.compare() method returns a number that tells about the comparison of two buffers. The number returned will be as follows −

  • 0 if the buffer objects are equal,

  • 1 if the first buffer object is greater than the second buffer object.

  • -1 if the first buffer object is less than the second buffer object.

It throws an error ERR_OUT_OF_RANGE if the targetStart and sourceStart are less than 0, also if targetEnd > target buffer.length and sourceEnd > comparedBuffer.length.

Example

In the example below we have created two buffers using the Buffer.from() method. The string used to create the buffer is hello.

Same string hello is used for both buffers. Buffer.compare() method is used to compare both buffers. Since the string is the same the output from below code should be 0.

const buffer1 = Buffer.from('hello');
const buffer2 = Buffer.from('hello');
const result = Buffer.compare(buffer1, buffer2);
console.log("The output is :"+result);

Output

The output is :0

Example

In this example we are going to make use of different strings in Buffer.from() and later use Buffer.compare() to see the output.

const buffer1 = Buffer.from('hello');
const buffer2 = Buffer.from('world');
const result = Buffer.compare(buffer1, buffer2);
console.log("The output is :"+result);

Output

In the example you will see the string used is hello and world. Both are compared and the output for above will be -1. It is because the buffer1 object is lower than the buffer2. As you see the start character of buffer1 is "h" comes before "w" and hence buffer1 one is smaller than buffer2.

The output is :-1

Example

In this example we are going to make use of a higher string value for buffer1 than buffer2.

const buffer1 = Buffer.from("world");
const buffer2 = Buffer.from("hello");
const result = Buffer.compare(buffer1, buffer2);
console.log("The output is: "+result); methods.

Output

The output is: 1

Example

In this example will make use of the params target, targetStart, targetEnd, sourceStart and sourceEnd.

const buffer1 = Buffer.from([11,12,13,14,1,2,3,4,5,6]);
const buffer2 = Buffer.from([1,2,3,4,5,6,7,8,11,12,13]);
const result = buffer1.compare(buffer2, 8, 11, 0, 3);
console.log("The output is :"+ result);

Output

In above case the target param: is buffer2

The targetStart param is 8, targetEnd param is 11 refers to buffer2. So the offset value 8 till 11 the values in buffer2 will be : [11,12,13]

The sourceStart and sourceEnd values refer to buffer Buffer1. So the value from 0 to 3 in buffer1 will be [11,12,13].

The result will be 0 as both values are equal.When you execute the above example the output will be as shown below −

The output is :0

Example

Let me take the example from above and change the values in array from buffer1 to as shown below −

const buffer1 = Buffer.from([111,12,13,14,1,2,3,4,5,6]);

I have changed the 11 to 111. The complete code is as shown below −

const buffer1 = Buffer.from([111,12,13,14,1,2,3,4,5,6]);
const buffer2 = Buffer.from([1,2,3,4,5,6,7,8,11,12,13]);
const result = buffer1.compare(buffer2, 8, 11, 0, 3);
console.log("The output is :"+ result);

Now the comparison output will be [111,12,13] and [11,12,13]. Since the buffer1 is greater than buffer2 the output will be 1.

Output

The output is :1

Example

Let us again change the array of buffer1 to a smaller value as shown below −

const buffer1 = Buffer.from([0,12,13,14,1,2,3,4,5,6]);

The complete code is as shown below −

const buffer1 = Buffer.from([0,12,13,14,1,2,3,4,5,6]);
const buffer2 = Buffer.from([1,2,3,4,5,6,7,8,11,12,13]);
const result = buffer1.compare(buffer2, 8, 11, 0, 3);
console.log("The output is :"+ result);

The output that is being compared is [0,12,13] and [11,12,13], now since buffer1 is less than buffer2 the output will be -1.

Output

The output is :-1
nodejs_buffer_module.htm
Advertisements