Context get and set performance

I am trying to structure some data I save to global context. When I do a global.get() in a function node, is a copy of the object made or just a reference returned? If a copy is made, it might be better to have a larger number of smaller global objects. Any impact on garbage collection?

I also have several motion axes which need to be "called" by different nodes. I can pass back a done condition using a message which includes an element identifying the axis but that message would be duplicated and sent to a number of nodes. The alternative is to use a global context variable. Again, I am thinking of overall performance.




Premature optimisation is often the cause of bad design or bugs. Go for the simplest /sensible design first. if you have performance issues then optimise.

Unless you are doing it thousands of times a second then don't worry about it. Even if you are doing it thousands of times a second then still don't worry about it, wait till you find whether you have a performance issue and if you do then work out where the bottlenecks are. I can virtually guarantee that they will not be where you expect.

1 Like

If global.get() just passes a reference, I would think just changing the value in the function node would change the global context but that does not appear to be the case.

I would love to see a feature that would allow one function node to send a message directly to another function node. This can be done using multiple outputs and link in/out or using global context, but it would be nice to have a node.sendTarget() function. Perhaps the target could be identified by its tab and name.

I would follow the advice to not worry about performance until I see where are the bottlenecks.


It will only be by reference if it is an object or array. For example, if it is an object then changing a property of the object will change it in context. However, it is not recommended to rely on that as if you use persistent context then node red will not know that it needs to persist the modified object, that is why you should always do global.set.

…Which defeats the purpose of a visual wiring paradigm, so no.

I knew that would be controversial, but global.get/set already allow node to node communication without a visual component.

Yes, but it is a passive sense of communication. One node can write to context and another node is free to read from context when it needs to. But there is not a direct relationship between those nodes.

What we wouldn't want is a more active node-to-node communication where a node can directly trigger another one.

1 Like

Yes, and it has been discussed before, in different contexts. If you follow the links from the discussion linked above, you'll see how the suggestion has been received. The "philosophical" objection raised by @dceejay is fundamental, and @knolleary is pointing to a lot of practical issues that can come up in understanding flows and troubleshooting them when message flow can become invisible and virtually untraceable.

Of course if you look hard enough you may find some contrib nodes that do this, and you can code whatever you like, but it won’t be in the core any time soon :slight_smile:

1 Like

Some (well myself anyway) consider that global and flow context should only be used in a very limited set of situations.

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