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).
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 flow, & context variables act differently to global variable in Node RED - at the process level they do not, it only denotes what can access them.
flow, context & global variables will all live in memory in the same way.
Why do you assume a global is not collected?
(remember a global variable is a Node RED concept - not a process concept)
if that is the thought pattern, then a flow & context variable also does not get collected.
any variable can be removed (regardless of scope) by setting it to undefined
global.set("someVar",undefined)
All variables will remain, until Node RED is terminated or you set the var to undefined
EDIT
Once again - I am not a pro here, but if global storage is different at the process level to a flow or 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.
Edit: nonsense! 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
@Colin
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?
Personally, I think you are reading into that article way too much - it's talking about javascript in general, not how Node RED offers variables across context.
You will have to store many many MB's of vars before having to worry about any performance impact (if at all)
if you need to access the same values across flows
use global vars
if you need to access the same values within the same flow only
use flow vars
If the vars never change
Use environment vars
OR if you are for some reason worried,
use a single context var inside a function, and access it via a link-call node
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.