Is there any reason you don’t want to flatten those helpers ?
With your version you save a couple of calls to the reactive variable but you are building functions on the fly which is probably just equally expensive.
The flat code is probably more idiomatic. The only case when I build functions on the fly in this setting is when the common computation is expensive (crunching data) and it’s hard to cache - otherwise you just compute it on the first call and just read the cache on subsequent calls.
Your second example (2 helpers) is better than the first one (4 helpers).
What you should avoid (unless you really know what you are doing) is to create functions on the fly like you were trying to do with the very first example
Template.profile.helpers({
avoid_unless_you_know_what_you_are_doing : () => {
const c = Template.instance().gender.get()
return {
f1 : v => (c == v) ? "equal" : "nonequal",
f2 : () => c
}
}
})
Everything is under user() because I want to pass user context and use it with {{#with user}} in my spacebars .html. Am I doing it wrong? Is use of {{#with}} inexpedient? Are these computations expensive in regards to the performance?
Do what works for you. What matters is that you easily understand and are able to maintain your code.
That said, I would have written it this way
Template.profile.helpers({
user : () => ({ // Just return data
username : "myname",
id : Template.instance().id.get(),
gender : "male"
}),
isMale : g => (g == "male"), // use data as parameter
})
<template name="profile">
{{#with user}} <!-- gets the data -->
<p>name : {{username}}</p> <!-- uses the data -->
<p>is male ? : {{isMale gender}}</p> <!-- passes the data to a helper -->
{{/with}}
</template>