You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the performance issue has not already been reported
Last performant version
4.9.9
Slowed down in version
4.9.9
Node.js version
v16.16.0
🦥 Performance issue
In the Mongoose lib CPU profiling, I noticed that the docReducer function present in document.js is consuming 30% of Application CPU resources. Upon further investigation, I found multiple calls to __getAllSubdocs during the document save call, which calls docReducer. This function recursively checks each key in the document to see if there are embedded documents. However, my collection does not contain embedded documents(embedded is not needed for my usecase), and I would like to avoid this overhead. Is there a flag to skip the docReducer call, or do you guys have any suggestions on how to handle this? Or what can be done here?
Shall i comment the code var subDocs = Object.keys(this._doc).reduce(docReducer.bind(this), []); and simply return []; ?
In the latest Mongo version docReducer function is optimized but its still there with big overhead.
`
Document.prototype.$__getAllSubdocs = function() {
DocumentArray || (DocumentArray = require('./types/documentarray'));
Embedded = Embedded || require('./types/embedded');
function docReducer(seed, path) {
var val = this[path];
if (val instanceof Embedded) {
seed.push(val);
}
if (val && val.$isSingleNested) {
seed = Object.keys(val._doc).reduce(docReducer.bind(val._doc), seed);
seed.push(val);
}
if (val && val.isMongooseDocumentArray) {
val.forEach(function _docReduce(doc) {
if (!doc || !doc._doc) {
return;
}
if (doc instanceof Embedded) {
seed.push(doc);
}
seed = Object.keys(doc._doc).reduce(docReducer.bind(doc._doc), seed);
});
} else if (val instanceof Document && val.$__isNested) {
if (val) {
seed = Object.keys(val).reduce(docReducer.bind(val), seed);
}
}
return seed;
}
var subDocs = Object.keys(this._doc).reduce(docReducer.bind(this), []);
return subDocs;
// shall i comment var subDocs = Object.keys(this._doc).reduce(docReducer.bind(this), []); ? and return []
};
`
Steps to Reproduce
calling mongoose save docReducer of __getAllSubdocs gets called
Expected Behavior
No response
The text was updated successfully, but these errors were encountered:
I checked and it looks like $getAllSubdocs() loops over every path in the document - we can optimize that because String paths will never have a subdocument for instance.
Also, another issue is that a successful save() calls $getAllSubdocs() 5 times:
getPathsToValidate()
saveSubdocsPreSave()
$__reset()
_setIsNew()
saveSubdocsPostSave()
That's definitely something we can optimize, there's no need to call $getAllSubdocs() 5 times.
Prerequisites
Last performant version
4.9.9
Slowed down in version
4.9.9
Node.js version
v16.16.0
🦥 Performance issue
In the Mongoose lib CPU profiling, I noticed that the docReducer function present in document.js is consuming 30% of Application CPU resources. Upon further investigation, I found multiple calls to __getAllSubdocs during the document save call, which calls docReducer. This function recursively checks each key in the document to see if there are embedded documents. However, my collection does not contain embedded documents(embedded is not needed for my usecase), and I would like to avoid this overhead. Is there a flag to skip the docReducer call, or do you guys have any suggestions on how to handle this? Or what can be done here?
Shall i comment the code var subDocs = Object.keys(this._doc).reduce(docReducer.bind(this), []); and simply return []; ?
In the latest Mongo version docReducer function is optimized but its still there with big overhead.
`
`
Steps to Reproduce
calling mongoose save docReducer of __getAllSubdocs gets called
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: