Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Meteor.userId() every time or should I store it in a locale variable?

In a function where I use several times the user id, what is the good practice:

  1. Declare a locale variable inside the function to store the userId for upcoming needs
  2. Use every time the related function (Meteor.userId() or in publications this.userId)

Another way to ask would be "is my locale variable a simple reference to the Meteor.userId() (calling it everytime) or is it a copy?

I agree that it might look as a micro-optimization, but I ask it more to learn and understand how Javascript works (in a meteor context).

Subsidiary question: Is it a question related to closure? I am still trying to figure that out.

like image 967
Billybobbonnet Avatar asked Jan 22 '26 03:01

Billybobbonnet


1 Answers

Meteor.userId() just returns a plain old Javascript string. You can save it in a variable after the first call to it and use it without any consequences.

This isn't related to closures per se, but it is related to how Meteor's reactivity. As long as you are calling Meteor.userId() at least once in your function in a reactive context, Meteor will re-run the function when it changes.

like image 151
musically_ut Avatar answered Jan 24 '26 17:01

musically_ut