# Comparing buffer

**URL:** <https://discourse.nodered.org/t/comparing-buffer/78294>\
**Category:** General\
**Created:** [9 May 2023 07:49 UTC](https://discourse.nodered.org/t/comparing-buffer/78294 "2023-05-09T07:49:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [9 May 2023 07:49 UTC](https://discourse.nodered.org/t/comparing-buffer/78294/1 "2023-05-09T07:49:48Z")

</div>

Hi,

How do you compare two buffers (using a function node)?

```auto
const decs1 = [3, 1, 1]
const buffer1 = Buffer.from(decs1);

const decs2 = [3, 1, 1]
const buffer2 = Buffer.from(decs2);

if (buffer1 != buffer2){
    msg.payload = 'true'
    return msg
} else{
    msg.payload = 'false'
    return msg
}

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [9 May 2023 07:59 UTC](https://discourse.nodered.org/t/comparing-buffer/78294/2 "2023-05-09T07:59:33Z")

</div>

I think you have the right idea.

But you need to clarify how this is being _triggered_.  
Usually the incoming message contains the things to be compared.  
Changing a property defines if it is _1_ or _2_.

`decs2` seems to get the values.  
But how that works is way above my head.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 May 2023 08:05 UTC](https://discourse.nodered.org/t/comparing-buffer/78294/3 "2023-05-09T08:05:42Z")

</div>

Google, or your favourite search engine, is often the easiest way to get answers to such questions. Then look for the w3schools or MDN link there if there is one.

> **[Node.js Buffer.compare() Method](https://www.w3schools.com/nodejs/met_buffer_compare.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [9 May 2023 09:49 UTC](https://discourse.nodered.org/t/comparing-buffer/78294/4 "2023-05-09T09:49:50Z")

</div>

Thank you @Colin

Just a bit confusing as if the statement is true it returned 0 and false it is 1, but nether the less a comparison and you can work with that.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 May 2023 10:20 UTC](https://discourse.nodered.org/t/comparing-buffer/78294/5 "2023-05-09T10:20:25Z")

</div>

It may return 1 or -1 if not equal, so in you case test for 0 or not zero. The reason for +-1 is so that buffers can be sorted into order.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 May 2023 10:20 UTC](https://discourse.nodered.org/t/comparing-buffer/78294/6 "2023-05-23T10:20:29Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
