# Coding suggestion

**URL:** https://discourse.nodered.org/t/coding-suggestion/43031
**Category:** General
**Created:** [22 March 2021 18:29 UTC](https://discourse.nodered.org/t/coding-suggestion/43031 "2021-03-22T18:29:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)
#### Post date: [22 March 2021 18:29 UTC](https://discourse.nodered.org/t/coding-suggestion/43031/1 "2021-03-22T18:29:13Z")

</div>

I need your best advise on finding the most efficient solution to my problem.  
Problem: I am monitoring an automation bus on which a stream of frames flows. The frames speed can range from 1/second to 15/second. I need to parse each one and send it to one out of 7 different outputs, in relation to its content.

An example could be:

```auto
if (payload.substr(0,6) === “*#34**”) {
    return[null,msg,null,null,null,null,null]
    }

```

As the project is quite large, I need to find the most efficient “parser” in terms of speed/performances.

What do you suggest? A simple function with all pieces of code like the one above, or something else?

---

<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: [22 March 2021 18:47 UTC](https://discourse.nodered.org/t/coding-suggestion/43031/2 "2021-03-22T18:47:21Z")

</div>

We need more information to answer. What is the general test? Is it different values of the same test, for example, or different bits of the string, or what?

---

<div class="post-metadata">

### Author: ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)
#### Post date: [22 March 2021 18:55 UTC](https://discourse.nodered.org/t/coding-suggestion/43031/3 "2021-03-22T18:55:01Z")

</div>

The test is what I am showing in the example. The single characters of the frame can change, as the substr value too (i.e. substr(2,5) ==“\*4#”)

---

<div class="post-metadata">

### Author: ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)
#### Post date: [22 March 2021 18:55 UTC](https://discourse.nodered.org/t/coding-suggestion/43031/4 "2021-03-22T18:55:55Z")

</div>

We need indeed more info, however if the search string is unique you can use the switch node and use "contains" to filter to 7 outputs.

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42d4e4b1fb280f03d4cf1bdd8de6160d9adb98fa.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: [22 March 2021 19:56 UTC](https://discourse.nodered.org/t/coding-suggestion/43031/5 "2021-03-22T19:56:45Z")

</div>

My first recommendation would be...

"The frames speed can range from 1/second to 15/second." \<\<\< that is a miniscule number of operations. [Premature optimisation is the root of all evil](https://www.google.com/search?q=Premature+optimisation+is+the+root+of+all+evil)  
Basically, dont worry about this until it is a problem

Without seeing all your code, it is really difficult to comment without **much speculation** but my 2nd recommendation would be, stop with all this frames stuff. Get your data into to native types (bools / numbers etc) in object properties and pass the object instead of a string where you are doing things like this "`payload.substr(0,6) === “*#34**”`"

e.g.. If the #34 is really a ID number (or a type number) then parse the string into a object with an ID property as soon as it is received into your node-red. There are multiple good reasons to do this (note there are caveats to what i say next but forgetting that for now, they are a good rule of thumb)...

1. Strings are immutable & thus always copied. Where as an object is passed by reference (aka faster)
2. numbers compare faster than strings (mostly)
3. comparing the value of an objects property named `.id` property is FAR faster than calling `.substr`

Lastly, if you are responsible for the shape of this data `“*#34**”` then stop that too. Send JSON - its far faster to reconstitute into a native JS object (no substring checks to worry about etc).

My get out clause for all the wrong things I just said - I did pre-warn on the speculation but you provide sooo little detail in your posts.

---

<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: [21 May 2021 19:57 UTC](https://discourse.nodered.org/t/coding-suggestion/43031/6 "2021-05-21T19:57:29Z")

</div>

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