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

Use mongodb exported bson instead of mongoose to import bson separately? #15154

Open
2 tasks done
zurmokeeper opened this issue Jan 3, 2025 · 4 comments
Open
2 tasks done
Labels
discussion If you have any thoughts or comments on this issue, please share them! help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Milestone

Comments

@zurmokeeper
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

When using chrome's memory to do a snapshot analysis of node.js references, I found that the bson library takes up a lot of memory, about 3-4M (estimated), especially if the project may reference multiple bson libraries.

I see that mongodb already has an API for exporting bson, so why does mongoose have to reference bson separately?

If I use mongodb's bson, then I can reference a mongoose, and I don't need to bring in mongodb and its dependent bson libraries.

It would be more memory efficient.

Motivation

No response

Example

No response

@zurmokeeper zurmokeeper added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Jan 3, 2025
@vkarpov15
Copy link
Collaborator

Mongoose imports bson directly rather than going through mongodb.BSON is for the browser build: we haven't tried recently, but importing the MongoDB driver makes it impossible for Mongoose to run in the browser.

We do try to keep the exact same version of bson as the MongoDB driver, so most package managers should automatically share the bson version between Mongoose and MongoDB.

Can you please run npm list bson and paste the output here? That would be helpful.

@vkarpov15 vkarpov15 added discussion If you have any thoughts or comments on this issue, please share them! help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed new feature This change adds new functionality, like a new method or class enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature labels Jan 6, 2025
@zurmokeeper
Copy link
Author

Image

We do try to keep the exact same version of bson as the MongoDB driver, so most package managers should automatically share the bson version between Mongoose and MongoDB.

I understand this, but if you use pnpm, the result is different. pnpm doesn't raise the dependencies to the top level with npm/yarn, it's still in the node_modules of the dependencies, so there's a lot of copies of different versions of bson, and since pnpm is getting more and more popular, and I'm using pnpm for my project, I just found this out. The memory footprint under pnpm is still noticeable.

I think there are two ways to deal with the problem you mentioned about the browser not working:
1: Determine if it's a browser or node, and then load it separately.
2: I'd like to see what's causing the browser not to work, and see what I can do about it so that the browser can support it.

Not sure about your comments and suggestions?
@vkarpov15

@vkarpov15 vkarpov15 added this to the 8.9.4 milestone Jan 7, 2025
@vkarpov15
Copy link
Collaborator

Option (1) is tricky because Webpack will pull in whatever we require() in, so it isn't as simple as if (typeof window !== 'undefined') { require('bson') } else { require('mongodb') } . And even a single import of mongodb anywhere in the browser fails with Uncaught TypeError: can't convert undefined to object because of an issue in whatwg-url. Even if we got mongodb working in the browser, the size difference between Mongoose's browser build with mongodb vs with just bson is 3MB vs 800KB.

Image

What we could do is move all type imports into the driver layer, so lib/drivers/browser/objectid exports bson.ObjectId and lib/drivers/node-mongodb-native/objectid exports mongodb.ObjectId. This is a possibility, but would require substantial refactor because types would have to be scoped to a Mongoose instance, which means helpers that use types, like lib/helpers/clone, would also have to be scoped to a Mongoose instance. We've experimented with this course of action, but it's a long road.

In the meantime, have you considered just using require('mongoose').mongo instead of including mongodb as a top-level dependency? I know that's a pretty similar suggestion to yours, but that should knock out at least one bson instance.

@vkarpov15 vkarpov15 modified the milestones: 8.9.4, Parking Lot Jan 7, 2025
@zurmokeeper
Copy link
Author

@vkarpov15

You are right. It looks like the only way to deal with this is from a code isolation point of view.

Separate the browser from the node.js entry code or places that need to use bson, make sure that node.js references mongodb.bson, and the browser only references the bson library, so that even if mongoose installs bson separately in the node.js environment, it won't reference it.

On a side note:

require('mongoose').mongo

Even if it does, it won't be missing an instance of bson, I looked at the source code

/index.js
const mongoose = require('. /lib/');

/lib/index.js
const mongoose = require('. /mongoose');

lib/mongoose.js
const utils = require('. /utils'); // with this reference

lib/utils.js
const UUID = require('bson').UUID; // This is where bson will be loaded.

So there shouldn't be an instance of bson missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them! help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants