# Optimize js function to convert decimal to 8 outputs

**URL:** <https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305>\
**Category:** General\
**Created:** [6 November 2021 18:26 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305 "2021-11-06T18:26:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [6 November 2021 18:26 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305/1 "2021-11-06T18:26:54Z")

</div>

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:

```auto
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;

```

---

<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:** [6 November 2021 19:09 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305/2 "2021-11-06T19:09:03Z")

</div>

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](https://flows.nodered.org/flow/63f29373e826a9fd4943d679d43e725f) around various nodes in you flow.

My bet is that function takes less than 4ms

---

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [6 November 2021 19:34 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305/3 "2021-11-06T19:34:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [7 November 2021 10:48 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305/4 "2021-11-07T10:48:42Z")

</div>

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](https://discourse.nodered.org/t/node-red-contrib-mcp23017chip/37999/71)

---

<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:** [8 November 2021 09:34 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305/5 "2021-11-08T09:34:08Z")

</div>

> [@xjust](#):
>
> I get about 20-30% when running it about every 2 seconds

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?

---

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [8 November 2021 10:33 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305/6 "2021-11-08T10:33:43Z")

</div>

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

---

<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:** [7 January 2022 10:34 UTC](https://discourse.nodered.org/t/optimize-js-function-to-convert-decimal-to-8-outputs/53305/7 "2022-01-07T10:34:18Z")

</div>

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