I’ve just finished reading Josh Owens blog about collection helpers and it got me thinking.
I’m in the process of designing my model for a quoting system. I’ve been wrestling with various ideas of how to deal with displaying the total of the quote. The quotes are likely to be very complex so I was looking at calculating the totals and margins whenever the quote items were modified and storing them in the quote document, using a combination of collection-hooks and autoValues.
This would mean the calculations were done by the server once for each quote, but often while the quote is being modified. The idea of attaching a helper to the quote collection that would return the results of the calculation was dismissed initially as I was reading this, because it would require the calculation to be run every time a user requested that total value. (A list of 100 quotes would contain a total for each quote for example). However, it then occurred to me that as all this data is available to the client anyway, it would be the client browser running these calculations and not the server anyway would it not?
I wonder whether this more distributed way might be more efficient overall. Has anyone with a bit more experience in Meteor than me got any thoughts on this?
I hope I’ve explained myself well enough.
Most of the time when profiling a Meteor app, the DOM operations and Minimongo access are what take the most for the CPU. I don’t think putting calculations in your model would be a problem, but only profiling the actual code could give a definitive answer.
I would also let the client do the calculation (if security of this operation is not a concern).
I’m still pretty new to meteor so I may need a little help understanding.
I have a folder called models that sits on the root (not in client or server)
This is where I’ve declared my schemas and collections, and also my collection helpers.
If one of these helpers contained a complex calculation, would that be run in either client or server depending on who called it?
Could I put these helpers in the server only and would their results still be available to the client (served as a static property of the collection) or would that not work?
My understanding of dburles:collection-helpers is that they provide some syntactic sugar around standard collection transforms. This means that the computation of the helpers (transforms) is done at the point each document is retrieved from its cursor. They will not add any overhead to the database or data down the wire (DDP).
Normally, therefore, you would declare them globally. The CPU to process them would only be incurred at the point (client or server) where they were used (e.g. referenced in a Blaze template on the client).
Okay this makes sense. I think I will continue building the data model and do some testing with some dummy data, perhaps comparing both options.
The thing to keep in mind is that only JSON (and EJSON) data can transit through the wire (DDP). So as explained by Rob Fallows any transformation has to happen after that (= on the client) when you play with a “rich” (= with methods) object on the client.