# Global variable with multiple nested objects

**URL:** https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279
**Category:** General
**Tags:** context
**Created:** [5 July 2024 05:44 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279 "2024-07-05T05:44:24Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![pbhanu368](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pbhanu368](https://discourse.nodered.org/u/pbhanu368)
#### Post date: [5 July 2024 05:44 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/1 "2024-07-05T05:44:24Z")

</div>

## I've a query, while writing and reading the data to the global variable having multiple nested objects from different flows and nodes ...will it leads to any performance issue in node red ? my global variable will be like this --\>

## {"vsecc":{"connector1":{"em":{"freq":{"real":{"value":{"type":{"int":{"value":{"data":{"data":{"data":{"data":{"data":{"data":{"data4":20,"data5":20,"data6":20,"data7":20,"data3":20,"data2":20,"data8":20,"data1":20,"data9":20,"data10":20,"data11":20}}}}}}}}}}}}}}}}

For our project I'm using the centralized data in one global variable under many layers of data splitted into sub objects, is this better approach to store data under single variable ?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [5 July 2024 06:44 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/2 "2024-07-05T06:44:00Z")

</div>

There will be a "cost" once objects become very large (objects with 100k+ arrays). The example you gave is very small, there will be no measurable performance impact. However, you could run into potential problems when multiple flows write into the same object and it is written to disk while other flows are reading+writing into the same object.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [5 July 2024 06:56 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/3 "2024-07-05T06:56:03Z")

</div>

> [@pbhanu368](#):
>
> {"vsecc":{"connector1":{"em":{"freq":{"real":{"value":{"type":{"int":{"value":{"data":{"data":{"data":{"data":{"data":{"data":{"data4":20,"data5":20,"data6":20,"data7":20,"data3":20,"data2":20,"data8":20,"data1":20,"data9":20,"data10":20,"data11":20}}}}}}}}}}}}}}}}

Maybe I misunderstand but doesn't that mean that your value "data7", already rather cryptically named, will be stored at `globalvariable.vsecc.connector1.em.freq.real.value.type.int.value.data.data.data.data.data.data.data7`?

Do you _need_ to use a global variable to store this value anyway?

---

<div class="post-metadata">

### Author: ![pbhanu368](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pbhanu368](https://discourse.nodered.org/u/pbhanu368)
#### Post date: [5 July 2024 08:12 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/4 "2024-07-05T08:12:31Z")

</div>

I just have given an example data structure, how I'm storing the different variables under single.  
I just need info about like will it effect the performance of node red (CPU usage) while accesing the global variable having length nested objectsand sub objects simultaeously from different nodes

---

<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: [5 July 2024 08:25 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/5 "2024-07-05T08:25:51Z")

</div>

How are you getting, modifying and setting the data? A short example of the code you are using please.

Are you using persistent context (so the data persists over a node-red restart)?

---

<div class="post-metadata">

### Author: ![pbhanu368](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pbhanu368](https://discourse.nodered.org/u/pbhanu368)
#### Post date: [5 July 2024 08:28 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/6 "2024-07-05T08:28:51Z")

</div>

No, I'm not using the persistent storage..I'm just using the runtime storage

---

<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: [5 July 2024 09:02 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/7 "2024-07-05T09:02:26Z")

</div>

And the other question?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [5 July 2024 09:12 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/8 "2024-07-05T09:12:37Z")

</div>

> [@pbhanu368](#):
>
> I just need info. Will it effect the performance of node red (CPU usage) while accesing the global variable having length nested objectsand sub objects simultaeously from different nodes

No it won't.

---

<div class="post-metadata">

### Author: ![pbhanu368](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pbhanu368](https://discourse.nodered.org/u/pbhanu368)
#### Post date: [5 July 2024 10:08 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/9 "2024-07-05T10:08:10Z")

</div>

Thanks, but someone had commented above like reading+ writing on to the same variable simultaneously ...creates issue

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 July 2024 10:11 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/10 "2024-07-05T10:11:03Z")

</div>

nodejs is single threaded so a simultaneous read write is not going to happen, as each node has to take its turn.

---

<div class="post-metadata">

### Author: ![pbhanu368](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pbhanu368](https://discourse.nodered.org/u/pbhanu368)
#### Post date: [5 July 2024 10:23 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/11 "2024-07-05T10:23:51Z")

</div>

So, updating the same variable from different nodes at the same time using common trigger for all the function node where each function node will set the same global variable...so in this case also, it wont be a problem ??

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/62a6d553c570e1416cb008af146363eae4e05f1e.png)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 July 2024 10:30 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/12 "2024-07-05T10:30:55Z")

</div>

your question is about writing to context, each node has to take its turn, so writing should not be an issue. You may find concurrency issues with the data not being what you expect, if two nodes have written to context, the first node that writes the data may find the data has been changed by the second node, and this may cause issues you may not of expected.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [5 July 2024 10:44 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/13 "2024-07-05T10:44:59Z")

</div>

> [@jbudd](#):
>
> No it won't.

> [@pbhanu368](#):
>
> Thanks, but somebody commented reading+ writing on to the same variable simultaneously ...creates issue

That's true but you made it clear that you only want to know if it will affect CPU usage so that's the question I answered.

---

<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: [5 July 2024 15:16 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/14 "2024-07-05T15:16:05Z")

</div>

> [@pbhanu368](#):
>
> So, updating the same variable from different nodes at the same time using common trigger for all the function node where each function node will set the same global variable...so in this case also, it wont be a problem

If you trigger them all at once and they all update the same variable, then they will run one after another, so at the end the variable will be set to whichever function ran last, but you will not know which one ran last.

---

<div class="post-metadata">

### Author: ![pbhanu368](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pbhanu368](https://discourse.nodered.org/u/pbhanu368)
#### Post date: [17 July 2024 16:45 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/15 "2024-07-17T16:45:31Z")

</div>

👍Thanks for the information !

---

<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: [15 October 2024 16:45 UTC](https://discourse.nodered.org/t/global-variable-with-multiple-nested-objects/89279/16 "2024-10-15T16:45:38Z")

</div>

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