Reputation: 6853
When I want to iterate over some set of documents stored in MongoDB from my Meteor app, I can use either
db.collection.find( ... ).forEach( function f( doc ){ ... })
or
var docs = db.collection.find( ... ).fetch();
_.each( docs, function f( doc ){ ... }); // using underscore.js
Which way is preferable from performance point of view? What cons and pros exist for both choices?
Upvotes: 4
Views: 4918
Reputation: 50416
The two statements do basically the same thing at the core API level, which is get the cursor and tranform the results. There is however one "core" difference about performance:
.forEach()
will tranform the cursor results "one at a time" and process the iterator function it is provided.
.fetch()
on the other hand gets the "array" from the cursor "all at once" which means it is all "in memory" in one hit.
So no matter what, neither is actually "faster" at doing the "core" operation of fetching from the cursor of the query. However, not evaluating an "expression" on each cursor iteration "may be" slightly faster, so .fetch()
might win a "little" here.
The great big catch of course is "everything is now in memory", so there is the "overhead" of doing that to consider. As well, at the time of .fetch()
the _.each()
is yet to be processed, so as the saying goes. "What you "gain" on the swings you likely "loose" on the roundabouts".
Without full benchmarking of specific cases, the "timings" are likely to be similar. And generic benchmarking is next to useless unless it specifically applies the size of data sets you are working with.
The general case therefore comes out, "About the same", However using .fetch()
is going to consume more memory than just iterating a cursor from the start.
Upvotes: 3