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