How fast loggedInUser() function is?

I have some UI elements that depends on current user.

Should I use loggedInUser() function every time or it's better to have it saved within load() function and passing as a parameter into inner rules?

  Discussion posts and replies are publicly visible

  • Hi Sergeiz,

    I would say it's better to save in local variable and re-use in all places.
    Please paste the below code in expression rule and check time taken.
    CODE 1:

    with(
    local!userLogged: loggedInUser(),
    local!email: user(
    local!userLogged,
    "email"
    ),
    local!firstName: user(
    local!userLogged,
    "firstName"
    ),
    {
    local!email,
    local!firstName
    }
    )

    Time take : <5ms

    CODE 2:

    with(
    local!userLogged: loggedInUser(),
    local!email: user(
    loggedInUser(),
    "email"
    ),
    local!firstName: user(
    loggedInUser(),
    "firstName"
    ),
    {
    local!email,
    local!firstName
    }
    )

    Time Taken: 5ms

    Hope this helps.
  • I like how   explains it.:
    In general, smart reuse of load variables is an important design practice to be mindful of as unnecessary reevaluation of the same expression or function comes with a cost.

    Another common offender is over use of fn!isUserMemberOfGroup(). This function can get expensive when it's overused - if a design has this in multiple sections on the same interface, or multiple interfaces in the same process, it might be a good idea to consider how you can test for this early, once, and retain the answer for later use.

  • 0
    Certified Lead Developer
    I would also add that using a local variable can be very helpful when it comes time to test - in other words, if you need to test a rule or interface's behavior when it sees a specific username, if you have "loggedinuser()" at several places in your rule/interface, you would need to replace those with the username you want to test, then undo that later. If you have 1 local variable set, however, you just need to quickly swap in the username to test, then switch it back, all in that one spot.