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 bug has not already been reported
Mongoose version
7.4.0
Node.js version
14.x
MongoDB server version
4.4
Typescript version (if applicable)
No response
Description
In Mongoose 5 the getters array for the "id" virtual was deduped. In Mongoose 6+ this no longer happens.
In Mongoose 6+, If models are repeatedly recreated, a new idGetter is added to the getters array with each model recreation. In my app, after a load test, the "id" getters array can grow to thousands of redundant idGetter functions. We use lean virtuals, so those thousands of functions are executed for every new object, and performance is severely degraded after a while.
Use case: we hot load and dynamically recreate models very frequently in our app.
In Mongoose 5 when a model was compiled there was a check to ensure that redundant functions didn't get added to the "id" virtual getter array:
I'd expect Mongoose to do a 5.x-type sanity check to prevent duplicate functions from proliferating in virtual getter arrays. Something like this would fix the issue:
VirtualType.prototype.get = function(fn) {
for (const getter of this.getters) {
if (getter === fn) {
return this;
}
}
this.getters.push(fn);
return this;
};
The text was updated successfully, but these errors were encountered:
Prerequisites
Mongoose version
7.4.0
Node.js version
14.x
MongoDB server version
4.4
Typescript version (if applicable)
No response
Description
In Mongoose 5 the getters array for the "id" virtual was deduped. In Mongoose 6+ this no longer happens.
In Mongoose 6+, If models are repeatedly recreated, a new idGetter is added to the getters array with each model recreation. In my app, after a load test, the "id" getters array can grow to thousands of redundant idGetter functions. We use lean virtuals, so those thousands of functions are executed for every new object, and performance is severely degraded after a while.
Use case: we hot load and dynamically recreate models very frequently in our app.
In Mongoose 5 when a model was compiled there was a check to ensure that redundant functions didn't get added to the "id" virtual getter array:
This check does not appear to exist in 6+, so the array can grow dramatically with redundant functions when models are regularly recreated.
Steps to Reproduce
I added some console output in the mongoose source code, Virtualtype get method:
The output of the last few iterations of the loop in the script above show the "id" getters array growing with duplicate idGetter functions:
Expected Behavior
I'd expect Mongoose to do a 5.x-type sanity check to prevent duplicate functions from proliferating in virtual getter arrays. Something like this would fix the issue:
The text was updated successfully, but these errors were encountered: