Optimize js function to convert decimal to 8 outputs

I have 6 instances of snmp requests that converts the output (decimal value) to each of the 8 outputs (true/false) using the function below.
All of these outputs go to switches inside dashboard.

Anybody can help me make this a little bit more cpu friendly ? I get about 20-30% when running it about every 2 seconds (with 3-400ms between requests)

function is this:

let myFunc = num => Boolean(Number(num));

var myInt = parseInt(msg.payload[0].value,10).toString(2).padStart(8, '0');
var intArr = Array.from(String(myInt), myFunc).reverse();

var msg12 = intArr.map(function (p) {
    return {payload: p};
});
node.status({text:msg.payload[0].value});
return msg12;

I very much doubt this function is causing spikes like that. Unless the payload array is in many thousands of elements.

Try adding some flow timers around various nodes in you flow.

My bet is that function takes less than 4ms

You are right.
I think the spike is caused by the dashboard switches receiving many inputs.
I blocked each of the outputs of the function with RBE to block statuses that are the same and its a little bit better now.

I agree with @Steve-Mcl . This whole code takes max 1000 CPU cycles to run.
(at 1GHz > that is 0.001 ms)

But a good programmer always optimising everything :slight_smile:
You can store min. 31 True/False values safely in 1 number. (No need of Arrays)
I recommend these bit manipulation functions wrote by @Steve-Mcl :
Node-red-contrib-mcp23017chip - #71 by Steve-Mcl

What hardware are you running on and what OS?

Are you running the browser on the same machine that is running node-red?

What process is showing as consuming the processor when you run top, if it is linux, or whatever is a similar thing in your OS?

I am running it on a raspberry pi.

The issue is clearly not caused by the javascript, but snmp read node before it and perhaps the dashboard switches outputs after.
Top process is 'node'.

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