Perfomance between looping through cursor and fetch and loop through array

My use case is I want to send weekly summary email on monday to every user, I’ve thousands of users and expecting lot more to join.

Is there any perfomance difference between looping through results using cursor like below

 const users = Meteor.users.find({});

  if (users && users.count() > 0) {
    users.forEach((user) => {
      console.log(user);
    });
  }

and this

 const users = Meteor.users.find({}).fetch();

  if (users && users.length > 0) {
    users.forEach((user) => {
      console.log(user);
    });
  }

I think in the first case as we’re not fetching all records, query will be faster but I worry that when we’re looping through cursor, for every loop we’re cursor fetches that single record thus doing too many calls.

Which is the best way for my case? Any help appreciated, thanks.

1 Like

I haven’t done any benchmark yet but I think if you have a lots of ram then go to fetch() them all, the array will be stored in memory.
I will go by looping cursor instead of fetch() them all. And you should query only fields you need.