Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow creation of documents with 100+ fields #11757

Open
shyrik13 opened this issue May 3, 2022 · 3 comments
Open

Slow creation of documents with 100+ fields #11757

shyrik13 opened this issue May 3, 2022 · 3 comments

Comments

@shyrik13
Copy link

shyrik13 commented May 3, 2022

Question
We store mongoose documents objects in Redis as JSON.
We transform that objects to documents like this:

// const redisResponse = response from Redis
const docs = {};

for (let item in redisResponse) {
    let doc = new myModel(JSON.parse(item));
    docs[doc.id] = doc;
}

In item we have approximately 100 fields included embedded object.
The problem is that when we have 500 objects it takes 3 seconds to build all documents from that cached objects.
We have give to our NodeJs instance 500m (millicpu) CPU and 2048Mi RAM.

@Uzlopak
Copy link
Collaborator

Uzlopak commented May 3, 2022

Why do you need the hydrated Objects? Hydrated Object always mean Overhead. If you just want to revive e.g. ObjectIds, than you could try to store the rawResult of your mongo calls into redis and use the bson deserializer instead of JSON.parse.

Why do you use an Array to store the hydrated objects? v8 probably also slows there down. It would be probably better to store the hydrated Objects in an Object or even better in a Map. Having "holes" in the Array keys reduces the v8 performance significantly. But If you want to keep your Array, you should atleast initialize the length of your docs Array with a length.

Like:
const docs = new Array(redisResponse.length);

Cross that. You cant initialize the length.

@Uzlopak Uzlopak added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label May 3, 2022
@molarro
Copy link

molarro commented May 5, 2022

We store documents in Redis as hash, where id is a key, but value as document content in JSON. Periodically we need to get all documents from Redis and transform (hydrate) to Mongoose documents, because we use built-in and custom methods of document object, as well as id generation for new embedded objects. So JSON.parse is quite fast, unlike new myModel .

@vkarpov15 vkarpov15 added this to the 6.3.6 milestone May 11, 2022
@vkarpov15 vkarpov15 added performance and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels May 11, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.3.6, 6.3.7 Jun 7, 2022
@vkarpov15
Copy link
Collaborator

I belive this is a duplicate of #11541. We're actively working on improving this.

@vkarpov15 vkarpov15 removed this from the 6.3.7 milestone Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants