How to make Mongoose hydration faster? 📈 #11683
rubenvereecken
started this conversation in
General
Replies: 1 comment 1 reply
-
We're actively working on some issues related to this. See #11541, #10400, etc. Minimizing performance overhead and memory usage has been our top priority over the last year or so. We've made some good progress, but we still have a ways to go. If you're willing to provide us the code of the benchmark you ran, that would be helpful. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We've got a quickly growing project so we're reevaluating some assumptions that we made when we started the project. One of the reasons is that performance is becoming a concern.
We already knew Mongoose was faster if we didn't hydrate results (using
query.lean()
), but we heavily underestimated how much faster. A simple benchmark where we fetch a 36kb document (containing subdocuments), then serialise it to json (30 iterations in each case):Granted, it's a big document, but over 20ms on an application server is a massive performance price to pay. This CPU spend was also validated using tracing — it all happens in the Mongoose layer, the database call is the same.
We're relying hard on hydrated documents because we have some nice transformation logic and quite a lot of methods that require hydrated documents. Though, not really. Really what we need are lean documents with a thin layer of logic on top — something we thought Mongoose provided. There's some good news: there's a way out for us. Using lean +
mongoose-lean-methods
got us some of the way there (no noticeable performance impact), but it's a big undertaking.Finally, the question, an invitation for discussion: where is the majority of this time spent? It's completely blocking so it's nothing IO-related. Would there be a way for us to limit this expensive computation? Is it really necessary? Even thinking of all the goodies Mongoose offers and having a skim of
document.js
, I just can't think of where 20ms might be spent.Ideally what we have is all of the simple stuff Mongoose offers: transformations, methods, virtuals etc, which are all very cheap, and ignore whatever turns out to be so expensive.
Beta Was this translation helpful? Give feedback.
All reactions