Do Helpers cache Cursors?

Hi!

I have a Template that contains a select in it which enables the user to filter through some Objects in a collection that are displayed on a table.

I store the result of the select in a ReactiveVar. I then use this ReactiveVar in the query for a Helper to return my Objects to the Template.

Template Code

<select id="colorSelect">
    <option val="getRed">getRed</option>
    <option val="getBlack">getBlack</option>
</select>

<table>
     {{#each Objects}}
          /* create table */
    {{/each}}
</table>

Here’s the JS

//onCreated initializing Template ReactiveVar for Object's Color
Template.Object.onCreated(function() {
    this.color = new ReactiveVar("");
});

//event for changing 'color' based on select option
Template.Object.events({
    'change #colorSelect'(e, template) {
         const target = e.target;
         const color = $(target).val();
         template.color.set(color);
   }
});

//Helper method to return Objects
Template.objects.helpers({
    Objects: function() {
        const color = Template.instance().color.get();
        return Objects.find({foo:bar, color:color}).fetch();
    }
});

This all seems to work just fine and my table adjusts reactively based on the select.
My question is about the efficiency and if the framework knows to cache a cursor I’ve already gotten. So would it literally do a DB.find() everytime the user changes the select?

Is it more efficient to

  1. Do one initial find
  2. Then filter on this main original data (an array from fetch call)
  3. Finally return back to UI
    However, doing it this way seems to lose any advantage of reactivity altogether and would require rearranging the table with JS.

Or…does Meteor just know to cache cursors I’ve already gotten (Does it even make a performance difference - seeing as it’s taking it from MiniMongo…which afaik is just a JSON object)

Is this pattern of using the reactiveVar in tandem with a Helper the standard?

That’s perfectly fine. AFAIK, Meteor won’t cache the cursors, but it caches the documents loaded from the server in its client-side MiniMongo DB. It’s so fast and offers all the Mongo query goodies that I would always prefer it over hand-written filtering code. And yes, you’re handling the ReactiveVars right.

Sweet! Thanks waldgeist for the reassurance. And yeah, much less tedious than writing own filtering code and then updating UI based on that