Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare Buffer objects in NodeJS?

Tags:

node.js

I am new to Node and start to play some node code. The problem I met is how to compare Buffer objects in NodeJS directly? Here "directly" means without using buffer.toString() method or iterate the whole buffer.

Here is an example:

var buf1 = new Buffer("abc");
var buf2 = new Buffer("abc");
console.log(buf1===buf2); //result is false
Buffer.compare(buf1,buf2);//lengthy error message

Thanks Derek

Update: I am using version "v0.10.38", here is the message if I use buf1.compare(buf2):

>buf1.compare(buf2)
TypeError: Object abc has no method 'compare'
at repl:1:7
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:203:10)
at Interface._line (readline.js:532:8)
at Interface._ttyWrite (readline.js:761:14)
at ReadStream.onkeypress (readline.js:100:10)
at ReadStream.emit (events.js:98:17)
at emitKey (readline.js:1096:12)
like image 327
derek Avatar asked Jun 08 '15 04:06

derek


People also ask

Which function is used to equate two node JS buffers?

compare() Method - GeeksforGeeks.

How do I decode a buffer in Node JS?

In Node. js, the Buffer. toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.

What is buffer object in node JS?

What Are Buffers? The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.


1 Answers

Per the nodejs change log, it looks like .compare() and .equals() were added in node v0.11.13.

I don't have explicit v0.10 doc that I can find, so maybe you'd have to write a byte by byte comparison yourself.

Here's a quick and dirty comparison function:

function areBuffersEqual(bufA, bufB) {
    var len = bufA.length;
    if (len !== bufB.length) {
        return false;
    }
    for (var i = 0; i < len; i++) {
        if (bufA.readUInt8(i) !== bufB.readUInt8(i)) {
            return false;
        }
    }
    return true;
}

FYI, in looking at the nodejs source code, the built-in .compare() or .equals() in the newer nodejs version will be a lot faster because they go to C and do memcmp() directly on the buffer which is going to be a lot faster than two method calls for every item in the buffer.


You can use any of these in node v0.12.2:

var buf1 = new Buffer("abc");
var buf2 = new Buffer("abc");
buf1.equals(buf2);            // returns true
buf1.compare(buf2).           // returns 0
Buffer.compare(buf1, buf2);   // returns 0

Here's more detail on each option:

var buf1 = new Buffer("abc");
var buf2 = new Buffer("abc");
console.log(buf1.compare(buf2));   // 0 means buffers are the same

The result will be 0 when the two buffers are identical non-zero if not.


You can also use:

var buf1 = new Buffer("abc");
var buf2 = new Buffer("abc");
console.log(buf1.equals(buf2));   // true means buffers are the same

To get a boolean back for whether the two buffers contain the same bytes.


FYI, your original code of:

var buf1 = new Buffer("abc");
var buf2 = new Buffer("abc");
Buffer.compare(buf1,buf2);

Works just fine for me. It returns 0 just like buf1.compare(buf2).


In Javascript the === operator for two objects compares to see if the two variables point to the exact same object, not whether the separate objects contain the same content. So, buf1 === buf1, but buf1 !== buf2.

like image 157
jfriend00 Avatar answered Oct 05 '22 09:10

jfriend00