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.
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:
[ <Buffer 32>, <Buffer 31>, <Buffer 33> ] [ <Buffer 31>, <Buffer 32>, <Buffer 33> ]
Note: The above program will compile and run by using the
node index.js command.
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs | DNS
- Web-Socket in NodeJS
- Nodejs | Jimp
- Nodejs | DNS | setServers()
- Nodejs | GM charcoal() Function
- Nodejs | GM sharpen() Function
- Nodejs | GM blur() Function
- Dockerizing a simple Nodejs app
- Nodejs | Web Crawling using Cheerio
- Difference between NodeJS and AngularJS
- Encrypting Data in NodeJS
- Signup Form Using Nodejs and MongoDB
- Nodejs – Connect Mysql with Node app
- Cowsay in Nodejs using Requests library
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.