# Performance get/set variables. different methods

**URL:** <https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 January 2023 15:33 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357 "2023-01-29T15:33:30Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [29 January 2023 22:59 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357/21 "2023-01-29T22:59:01Z")

</div>

If you need all the variables then not quite;

```auto
let {block_active, allow_rtc_onoff, allow_scheduler, allow_solar, block_mode, message_limit, counter, messages_yesterday, messages_today, block_mode_previous} = global.get( 'MQTT', "file").MQTT

```

The MQTT file is not the MQTT Object, just a container for the Object

```auto
[{"id":"a63d08342b71197c","type":"inject","z":"7fdabd9.f693544","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"MQTT\":{\"block_active\":true,\"allow_rtc_onoff\":\"on\",\"allow_scheduler\":true,\"allow_solar\":true,\"block_mode\":true,\"message_limit\":null,\"counter\":2,\"messages_yesterday\":20,\"messages_today\":2,\"block_mode_previous\":\"\"}}","payloadType":"json","x":2470,"y":420,"wires":[["dd6be678775335e5"]]},{"id":"dd6be678775335e5","type":"function","z":"7fdabd9.f693544","name":"function 12","func":"context.set(\"loadsOfThings\", msg.payload)\n\nlet {message_limit, allow_rtc_onoff} = context.get(\"loadsOfThings\").MQTT\nmsg.message_limit = message_limit\nmsg.allow_rtc_onoff = allow_rtc_onoff\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2670,"y":420,"wires":[["5ce9fa176e991281"]]},{"id":"5ce9fa176e991281","type":"debug","z":"7fdabd9.f693544","name":"debug 244","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2890,"y":420,"wires":[]}]

```

a quick demo to show what I mean. But other than that, yes, but only if you need them all and if so why not just

```auto
let MQTT = global.get('MQTT', 'file')

```

The advantage of `global.get( 'MQTT', "file").MQTT` is that it could be `global.get( 'loadsOfStuff', "file").MQTT` and `'loadsOfStuff'` can be many other Objects - `global.get( 'loadsOfStuff', "file").somethingImportant`

---

<div class="post-metadata">

**Author:** ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)\
**Post date:** [29 January 2023 23:16 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357/22 "2023-01-29T23:16:00Z")

</div>

This is good stuff. Thank you Buckskin !!!

I imported it and now I indeed see. This is very effective and easy to use. Less lines to write as well.  
Very usefull !! Thanks.

---

<div class="post-metadata">

**Author:** ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)\
**Post date:** [30 January 2023 21:16 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357/23 "2023-01-30T21:16:13Z")

</div>

Just a check.  
I had a good look at the stuff you gave me.  
Is this the correct way you mean, or can the two (let) lists be combined even?

```auto

let msg1={};

let {message_limit, allow_rtc_onoff} = global.get('MQTT',"file");
let {LOW, NORMAL} = global.get('NightReductionRoomSetpoint',"file");

msg1.message_limit = message_limit;
msg1.allow_rtc_onoff = allow_rtc_onoff;
msg1.LOW = LOW;
msg1.NORMAL = NORMAL;

return msg1;

```

The result looks like this now, which seems fine to me. 🙂

```auto
{
    "message_limit": 500,
    "allow_rtc_onoff": 1,
    "LOW": 18,
    "NORMAL": 21.6,
    "_msgid": "661c4966ea6a608a"
}

```

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [30 January 2023 22:54 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357/24 "2023-01-30T22:54:21Z")

</div>

> [@edterbak](#):
>
> ```auto
> msg1.message_limit = message_limit;
> msg1.allow_rtc_onoff = allow_rtc_onoff;
> msg1.LOW = LOW;
> msg1.NORMAL = NORMAL
> 
> ```

I cannot think of a way to reduce the (let) lists in one go (other than having all the globals in one Object) but the assignment to` msg.` can be shortened a bit

```auto
msg = {...msg, allow_rtc_onoff, message_limit, LOW, NORMAL}

```

or using msg1

```auto
let msg1 = {allow_rtc_onoff, message_limit, LOW, NORMAL}

```

You can also rename the incoming variables if required

```auto
let {LOW: lowSetPoint, NORMAL: normalSetPoint} = global.get('NightReductionRoomSetpoint',"file")

```

msg1 then becomes

```auto
let msg1 = {allow_rtc_onoff, message_limit, lowSetPoint, normalSetPoint}

```

Have a read of [Destructuring assignment - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment), hours of fun. 😉

---

<div class="post-metadata">

**Author:** ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)\
**Post date:** [4 February 2023 12:44 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357/25 "2023-02-04T12:44:05Z")

</div>

Thanks Buckskin

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [4 February 2023 14:32 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357/26 "2023-02-04T14:32:45Z")

</div>

Just because I cannot resist

```auto
[{"id":"a63d08342b71197c","type":"inject","z":"e1e49eccc952ba10","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"anotherPayload","v":"{\"LOW\": true, \"NORMAL\": false}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"block_active\":true,\"allow_rtc_onoff\":\"on\",\"allow_scheduler\":true,\"allow_solar\":true,\"block_mode\":true,\"message_limit\":100,\"counter\":2,\"messages_yesterday\":20,\"messages_today\":2,\"block_mode_previous\":\"\"}","payloadType":"json","x":1490,"y":400,"wires":[["dd6be678775335e5"]]},{"id":"dd6be678775335e5","type":"function","z":"e1e49eccc952ba10","name":"function 12","func":"context.set(\"loadsOfThings\", msg.payload)\ncontext.set(\"moreThings\", msg.anotherPayload)\n\n// One way\nlet myContext = {}\nObject.assign(myContext, context.get(\"loadsOfThings\"), context.get(\"moreThings\") )\nmsg.myContext = myContext\n\n// Or another\nlet { message_limit, allow_rtc_onoff, LOW: lowSetPoint, NORMAL: normalSetPoint } = Object.assign(context.get(\"loadsOfThings\"), context.get(\"moreThings\"))\n\nmsg = { ...msg, allow_rtc_onoff, message_limit, lowSetPoint, normalSetPoint }\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1690,"y":400,"wires":[["5ce9fa176e991281"]]},{"id":"5ce9fa176e991281","type":"debug","z":"e1e49eccc952ba10","name":"debug 244","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1910,"y":400,"wires":[]}]

```

The 2 `global.get`s into 1 variable at the same time

```auto
let { message_limit, allow_rtc_onoff, LOW: lowSetPoint, NORMAL: normalSetPoint } = Object.assign(context.get("loadsOfThings"), context.get("moreThings"))

```

---

<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:** [5 April 2023 14:33 UTC](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357/27 "2023-04-05T14:33:39Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/performance-get-set-variables-different-methods/74357.md?page=1)
