Coding suggestion

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:

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?

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?

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#”)

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.

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
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.

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