# Parallel nodes efficiency

**URL:** <https://discourse.nodered.org/t/parallel-nodes-efficiency/21400>\
**Category:** General\
**Created:** [7 February 2020 15:47 UTC](https://discourse.nodered.org/t/parallel-nodes-efficiency/21400 "2020-02-07T15:47:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuzmich](https://avatars.discourse-cdn.com/v4/letter/k/3be4f8/32.png) [@kuzmich](https://discourse.nodered.org/u/kuzmich)\
**Post date:** [7 February 2020 15:47 UTC](https://discourse.nodered.org/t/parallel-nodes-efficiency/21400/1 "2020-02-07T15:47:59Z")

</div>

Hello guys, how does Node-red calculate parallel nodes? If you have 100 parallel function nodes with a single if clause vs one node with 100 consecutive if's, which one would be faster?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 February 2020 16:09 UTC](https://discourse.nodered.org/t/parallel-nodes-efficiency/21400/2 "2020-02-07T16:09:24Z")

</div>

Node.js is single-threaded, so there is no immediate benefit to making your flows more parallel. In fact that can just make it more complicated as you then may need to join those parallel branches back together.

You also have the overhead of passing messages to each of those branches - particularly if they have to be cloned along the way.

In terms of the Function node, there is also a slight overhead involved with the sandbox used by the node (more typically a memory-usage overhead rather than performance through-put).

So I would always expect a single Function node with lots of if statements to be 'faster' than a flow with hundreds of Function nodes.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 February 2020 23:08 UTC](https://discourse.nodered.org/t/parallel-nodes-efficiency/21400/3 "2020-02-07T23:08:38Z")

</div>

Hi @kuzmich,

If this is an academic question the Nick has answered it 👍

If this is something you're doing or thinking of doing, you might want to post a small example, there is possibly a much better way of achieving what you're attempting to achieve.

---

<div class="post-metadata">

**Author:** ![kuzmich](https://avatars.discourse-cdn.com/v4/letter/k/3be4f8/32.png) [@kuzmich](https://discourse.nodered.org/u/kuzmich)\
**Post date:** [8 February 2020 03:29 UTC](https://discourse.nodered.org/t/parallel-nodes-efficiency/21400/4 "2020-02-08T03:29:27Z")

</div>

I'm implementing a Telegram bot into my automatisation and it uses inline keyboard. When i evaluate callback data, at some point i need to make the bot post 2 consecutive replies - one is "delete the keyboard" and second is the actual post with info. So now i'm choosing between these two variants, but i was wondering about efficiency if this "tree" becomes more complicated, so i made the post.  
P.S. number 100 was an obvious exaggeration to make things simpler 😉

 ![Безымянный](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bb14a8fa1eede8597639f41ede69a478fe3e796b.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 February 2020 09:31 UTC](https://discourse.nodered.org/t/parallel-nodes-efficiency/21400/5 "2020-02-08T09:31:42Z")

</div>

From a manageability POV, I would chose the 1st version (1 function to evaluate)

However instead of a large bunch of `if` `else if` or a large `switch case` I'd explore the possibility of a lookup.

E.g.

Inject (on start up) \> function (initialise lookup from fixed data or database)

This lookup would likely be an array of objects or a dictionary or an object of keys to object (a lookup) stored in context for quick retrieval

E.g....

```auto
let lu = {
 "help" : {
    "keyboard" : {...},
    "response" : {...},
  },
  "get main line status" : {
    "keyboard" : {...},
    "response" : {...},
  },
}
flow.set("lookup", lu);

```

And in your "evaluate callback" function, you can _simply_ lookup and process reply...

```auto
let userInput = msg.payload;
let lu = flow.get("lookup") || {};
let o = lu[userInput];
if(o){
  // Send msg with o.keyboard to set the correct keyboard
  // process the options in o.response object and send the reply to user
} else {
   // Send msg to set the 'What Do You Want To Do' keyboard
  // Send "Sorry, I didn't understand" response to user
}

```

In other words, the code in your function becomes pretty much an unchanging dynamic bit of code that operates on the settings set in context.

The advantages of this is you could end up building a web based dashboard or application that lets you update the lookup on the fly or pass the maintenance of lookup responses on to a non developer.

I know my response is idyllic / simplistic and quite possibly not suitable for your application but I hope it spurs you on to a light bulb moment.

---

<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:** [8 April 2020 09:31 UTC](https://discourse.nodered.org/t/parallel-nodes-efficiency/21400/6 "2020-04-08T09:31:45Z")

</div>

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