I know the difference between global, flow and context variable and I don't use the global variable because some time ago I had read that to reduce memory usage (Pi4), among other things, it was advisable not use global variables.
I don't find anymore the post on web and so I'd like to know if there are some problem to use global variables (I should use at least 20).
(yes, my English is terrible but I do my best)
Hi @Giamma - Your english is better than mine, and I'm British!
context variables are nothing more than the scope that they apply.
They will occupy the same amount of resource, and unless your storing file blobs in variables, I really wouldn't worry.
if storing variables at the global scope works for you, then carry on.
I am not a pro at the Node JS runtime, but if there is a difference, I'm sure it's negligible, and in most cases, won't get noticed.
As I said, I am not a pro - so others may correct me, if there is a measurable difference.
Avoid global variables: Do not use global variables.
They don’t get collected and consume a lot of memory.
Instead, use local variables or pass parameters
I think you may be assuming that
context variables act differently to
global variable in Node RED - at the process level they do not, it only denotes what can access them.
global variables will all live in memory in the same way.
Why do you assume a
global is not collected?
global variable is a Node RED concept - not a process concept)
if that is the thought pattern, then a
context variable also does not get collected.
any variable can be removed (regardless of scope) by setting it to
All variables will remain, until Node RED is terminated or you set the var to
Once again - I am not a pro here, but if
global storage is different at the process level to a
context storage, then I stand to be corrected.
Certainly a global context variable, once set, will be retained indefinitely.
A flow context variable seems to behave similarly, though I'm not sure how to demonstrate that.
A context variable local to a single function node can presumably be garbage collected once the function is finished and created anew when the function next starts.
I don't know if the memory actually does get freed.
It's generally good coding practise to limit the scope of variables, both for clarity and memory efficiency.
Similarly in a function it is better to declare a "variable" as const where possible and to use let in preference to var.
I had to test this...
The variable remains, as long as the function node is still deployed, so will remain alive during each execution of the function node
Yes, of course you are right, it is retained!
context.set/get is there specifically to retain values from message to message.
Or until the value is changed, in which case the memory containing the original value will be freed.
@Giamma, what are you thinking of storing in context that will consume a lot of memory?
No, nothing that will consume a lot of memory,
but I have a lot of variables and I pass the values between many flow through node link-out - link-in and I wanted to use global var to avoid many node link-out - link-in but that article don't recommends it.
Do you know another way to pass values between flow?
You will have to store many many MB's of vars before having to worry about any performance impact (if at all)
OR if you are for some reason worried,
use a single context var inside a function, and access it via a link-call node
I'll add some global variables and I will check the memory usage ...
Thank you .....
Link nodes. It is best to pass data via messages when you can and only use context where there is a real advantage. It is very easy to get race conditions using global and flow variables.