Inside a meteor template helper function, is there any difference in performance, number of re-renders, or anything else if I return the result of a find
vs a fetch
?
For example, the find approach:
Template.players.topScorers = function () { return Users.find({score: {$gt: 100}}, {sort: {score: -1}}); };
Or adding a fetch:
Template.players.topScorers = function () { return Users.find({score: {$gt: 100}}, {sort: {score: -1}}).fetch(); };
The find-only approach is what is currently in the docs, but I’ve seen lots of other people using fetch
.
Yes there is.
By using fetch you register a dependency on the entire query result set on the spot. By using find
and later on iterating using {{#each}}
a dependency is registered on every document separately. So when one document changes, only the relevant code is re-rendered. When using fetch
, changing any document in the result-set would re-render the entire scope in which you used fetch
.
For small result-sets it doesn't make any difference. For larger sets with frequent changes it could slow down computation and cause undesired visual artefacts.
I wrote a post which may help you understand it (it doesn't answer your question directly though)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With