roro_57
roro_57

Reputation: 600

Meteor template optimisation

I was looking at some meteor code, and I saw this :

Template.notifications.helpers({
  notifications: function() {
    return Notifications.find({userId: Meteor.userId(), read: false});
  },
  notificationCount: function(){
    return Notifications.find({userId: Meteor.userId(), read: false}).count();
  }
});

So I was wondering, Is this optimized ? I mean, will the mongo database execute two queries ? The server part ? the client part ? (mini mongo then)

Is it possible to use the previous result in the second function ? I tried with

notificationCount = function(){
    this.notifications.length; 
....

But it doesn't work, but maybe meteor remembers the previous result and uses it ? I will definitely, in my template, return a something.find() to have a cursor and afterwards return other variable with, for example : count, or filter it with fields or other stuff so I'm intereted by this question.

Any expert to explain me ? Thanks a lot meteor community :) !

Upvotes: 0

Views: 52

Answers (2)

Mário
Mário

Reputation: 1612

You're not performing 2 queries on the server, which ends up being the most critical place. When you subscribe to data, the data goes to a local database in the browser called MiniMongo. You can run as many queries as you want on the client, the data set (usually and let's hope so) is small and there isn't a noticeable performance penalty.

If you've some performance issues you can save the results of Notifications.find({userId: Meteor.userId(), read: false}) to Session or to another Reactive Dictionary. This will slightly improve the performance because you save the query time of Minimongo: parsing, searching, etc.

On the server, you should be as careful as possible. A bottleneck in the server might mean that your entire application isn't going to be as fast as wanted.

Read more about mini in memory databases: https://www.meteor.com/mini-databases

Upvotes: 1

pahan
pahan

Reputation: 2453

  1. yes. on the client side, not on the server side.
  2. you can use {{notifications.count}} on template.

Upvotes: 1

Related Questions