Node.js Buffer.compare() Method

13 Oct, 2021
Buffer is a temporary memory storage which stores the data when it is being moved from one place to another. It is like an array of integers. Buffer.compare() method compares the two given buffers.
Syntax:
buffer1.compare( targetBuffer, targetStart,
            targetEnd, sourceStart, sourceEnd )
Parameters: This method accepts five parameters as mentioned above and described below:
targetBuffer: This parameter holds the buffer which will be compared with other buffer.
targetStart: It refers to the starting index from which the elements of target buffer will begin comparing. Its default value is 0.
targetEnd: It refers to the index till which the elements of target buffer will be compared. The default value is 0.
sourceStart: The index in input buffer from which the comparing of values will start. The default value is 0.
sourceEnd: The index in input buffer till which the comparing of values will be done. The default value is buffer.length
Return Value: It returns a number indicating the difference in both buffers. The returns number are:
0: If they are equal.
1: If buffer1 is higher than buffer2 i.e. if buffer1 should come before buffer2 when sorted.
-1: If buffer2 is higher than buffer1 i.e. if buffer2 should come before buffer1 when sorted.
Below examples illustrate the Buffer.compare() Method in Node.js:
Example 1:
// Node.js program to demonstrate the  // Buffer.compare() Method      // Creating a buffervar buffer1 = Buffer.from('Geek');var buffer2 = Buffer.from('Geek');var op = Buffer.compare(buffer1, buffer2);console.log(op);  var buffer1 = Buffer.from('GFG');var buffer2 = Buffer.from('Python');var op = Buffer.compare(buffer1, buffer2);console.log(op);
Output:
0
-1
Example 2:
// Node.js program to demonstrate the  // Buffer.compare() Method      // Creating a buffervar buffer1 = Buffer.from('2');var buffer2 = Buffer.from('1');var buffer3 = Buffer.from('3');var array = [buffer1, buffer2, buffer3];  // Before sortingconsole.log(array);  // After sorting arrayconsole.log(array.sort(Buffer.compare));
Output:
[ <Buffer 32>, <Buffer 31>, <Buffer 33> ]
[ <Buffer 31>, <Buffer 32>, <Buffer 33> ]
Example 3:
// Node.js program to demonstrate the  // Buffer.compare() Method       var buffer1 = Buffer.from('GeeksOne');var buffer2 = Buffer.from('GeekTwo');   // Print: -1 as size of buffer1 starting // from index 4 is less than buffer2 sizevar op = buffer1.compare(buffer2, 4);  // Print: 1 as the size of buffer2 starting // from index 5 is less than size of buffer1// starting from 0th indexvar op1 = buffer1.compare(buffer2, 0, 7, 5);   console.log(op);console.log(op1);
Output:
-1
1
Note: The above program will compile and run by using the node index.js command.
Reference: https://nodejs.org/api/buffer.html#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend
Node.js-Buffer-module
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React