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 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)...
Strings are immutable & thus always copied. Where as an object is passed by reference (aka faster)
numbers compare faster than strings (mostly)
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.