How to check if two buffers are equal in Node.js?

September 28, 2020 - 1 min read

To check whether 2 Buffer instances are equal, We can use the equals() method in the Buffer Instance in Node.js.

Let's say we have 2 buffers, one made from the string Hello World and another buffer made from Hex code 48656c6c6f20576f726c64 ( this is a hex representation of the string Hello World 🦄).

// String buffer
const strBuff = Buffer.from("Hello World", "utf-8");

// Hex Buffer
const hexBuff = Buffer.from("48656c6c6f20576f726c64", "hex");

Now let's check if both buffers made from the string and the hex are equal using the equals() method in the strBuff buffer object.

// String buffer
const strBuff = Buffer.from("Hello World", "utf-8");

// Hex Buffer
const hexBuff = Buffer.from("48656c6c6f20576f726c64", "hex");

// check if both buffers are equal
const isEqual = strBuff.equals(hexBuff);

/*
    The method will return true
    because both buffers have the same content
    even though their encoding is different
*/
console.log(isEqual); // true
  • The method accepts a valid buffer as an argument.
  • It will return boolean true if the buffers are equal and false if not.

Feel free to share if you found this useful 😃.