Take this two snippets:
var Tasks = new Meteor.Collection('tasks');
var completed;
Tracker.autorun(function() {
completed = Tasks.find({ completed: true }).fetch();
});
var Tasks = new Meteor.Collection('tasks');
var completed = [];
Tasks.find({ completed: true; }).observe({
// applying actions on the array
})
- Is the second snipped faster than the first one?
- How does minimongo store it’s data internally?
1 Like
Why are you duplicating the data in a collection copied to client via pub/sub and a second time in an array?
MiniMongo keeps a cache of your subscription data.
You may get some speed efficiency from option 2, if your data set is pretty large.
(But it’s a good idea to keep it low)
If you are rendering these data, simply return the Mongo cursor from a template helper.
Blaze will do the option 2 for you.
@jamgold I’m not duplicating - those are two different implementations for the same thing. Just wanted to compare the pros and cons for each.
@arunoda Thanks for your answer!
I’ve seen that MiniMongo keeps a cache of all the docs from one collection but I couldn’t find anything regarding queries.
So please confirm that I’ve got this right:
- If I have
CompletedTasks
and UncompletedTasks
as subscriptions, MiniMongo will keep all the records in the same cache.
- Blaze helpers that return a cursor are handled with the observe method.
- Running a query, even in the Tracker, it’s not cached and it will do all the computations again.
Thanks,
Andrei
The reason I said “duplicating” is that the client has a local copy of the mongo collection, based on the subscription, so all you would have to do is have a helper that returns a cursor
Template.x.helpers({
tasks: function(completed) {
return Tasks.find({ completed:completed })
});
which you could call from your template as
<template name="x">
<h1>Completed Tasks</h1>
{{#each tasks true}}
{{>task this}}
{{/each}}
<h1>Uncomplete Tasks</h1>
{{#each tasks false}}
{{>task this}}
{{/each}}
</template>
What advantage would copying the contents of the collection into an array give?
I was running that code into an AngularJS application. So, my intended question was: does blaze run the fetch()
method every time or it observe for fine-grained changes?
I do not believe minimongo caches individual queries nor does it use indexes. This is fine because its running on your client anyways. If there’s enough data where that would make a noticeable performance difference, you’re publishing too much data to the client anyways.
Regarding using fetch vs cursor in Templates, use the cursor. It will enable Blaze to be smart about what parts of the DOM it updates when things change vs. just returning an array which will force Blaze to re-render everything in the #each
. That’s my understanding, at least.
1 Like