Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor use fetch or find in template helper functions?

Tags:

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.

like image 450
MrColes Avatar asked May 17 '13 05:05

MrColes


1 Answers

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)

like image 114
Xyand Avatar answered Oct 12 '22 08:10

Xyand