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

[Performance] performance difference between deno_mongo and node.js driver #304

Open
yuqiang-yuan opened this issue Nov 30, 2021 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@yuqiang-yuan
Copy link

I'm facing a performance issue between deno_mongo driver and official mongodb driver for Node.js. Here are the versions information of my test:

  • MongoDB server: 4.2.6, sharded database and collection. all the mongos and mongodb processes are located in the same host.
  • deno_mongo driver: 0.28.0
  • mongodb driver for Node.js: 4.2.0
  • deno: 1.16.3
  • Node.js: 12.19.0
  • OS: Ubuntu Desktop 20.04.3

My laptop connects to mongodb server via wired connection and I send a very simple query to get all the 16 documents from a small collection. My connection string is very simple: mongodb://172.16.1.95:27017

Here are the time used:

With deno_mongo driver:

deno: 16 documents returned from mongodb used: 103 ms
deno: 16 documents returned from mongodb used: 99 ms
deno: 16 documents returned from mongodb used: 94 ms
deno: 16 documents returned from mongodb used: 95 ms
deno: 16 documents returned from mongodb used: 96 ms
deno: 16 documents returned from mongodb used: 98 ms
deno: 16 documents returned from mongodb used: 94 ms
deno: 16 documents returned from mongodb used: 96 ms
deno: 16 documents returned from mongodb used: 95 ms
deno: 16 documents returned from mongodb used: 96 ms

With mongodb official driver for Node.js:

node.js: 16 documents returned from mongodb used: 11 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 2 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 4 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 6 ms
node.js: 16 documents returned from mongodb used: 4 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 4 ms

I'd like to know why is there such a significant performance difference between deno_mongo driver and official mongodb driver for Node.js. Or I did something wrong?

Here are the source code:

Deno:

import { Document, MongoClient } from "mongo/mod.ts";

const client = new MongoClient();

console.log('connecting to mongodb...');
await client.connect('mongodb://172.16.1.95:27017/');
console.log('mongodb connected');

const db = client.database('mqp');
const col = db.collection<Document>('dataDicts');

for (let i = 0; i < 10; i++) {
    const _ms = Date.now();
    const docs = await col.find({}).toArray();
    console.log(`deno: ${docs.length} documents returned from mongodb used: ${ Date.now() - _ms } ms`);
}

client.close();

Node.js:

const { MongoClient } = require('mongodb');

const url = 'mongodb://172.16.1.95:27017/';
const client = new MongoClient(url);

// Database Name
const dbName = 'mqp';

async function main() {
  // Use connect method to connect to the server
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('dataDicts');

  for (let i = 0; i < 10; i++) {
    const _ms = Date.now();
    const docs = await collection.find({}).toArray();
    console.log(`node.js: ${docs.length} documents returned from mongodb used: ${ Date.now() - _ms } ms`);
  }

  return 'done.';
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close());
@erfanium erfanium added enhancement New feature or request good first issue Good for newcomers labels Nov 30, 2021
@erfanium
Copy link
Member

erfanium commented Nov 30, 2021

This is an smaller repro for this bug

import { Document, MongoClient } from "./mod.ts";

const client = new MongoClient();

await client.connect("mongodb://localhost:27017");
console.log("mongodb connected");

const db = client.database("app");
const col = db.collection<Document>("events");

const t0 = performance.now();
await col.find({}).toArray();
const t1 = performance.now();
await col.find({}).toArray();
const t2 = performance.now();
await col.find({}).toArray();
const t3 = performance.now();

console.log(`A took ${t1 - t0} ms`);
console.log(`B took ${t2 - t1} ms`);
console.log(`C took ${t3 - t2} ms`);

and result:

A took 21.15469200000001 ms
B took 83.48546299999998 ms
C took 14.065189000000032 ms

node.js result:

A took 36 ms
B took 10 ms
C took 9 ms

@yuqiang-yuan
Copy link
Author

@erfanium Nice work! I've tested the new code and it took about 12ms for retrieving 16 documents.
But it's still 2~4 times slower than the node.js driver. It will be appreciated if you would keep tracking this.
Thanks and Regards,

@erfanium
Copy link
Member

erfanium commented Dec 2, 2021

@yuqiang-yuan
Thank you
yeah, there's work to be done about performance but i'm trying to keep everything in balance. some important features are still missing. so it would be nice if others could help about this issue

@erfanium erfanium changed the title [Performance] Why is there such a significant performance difference between deno_mongo driver and official mongodb driver for Node.js? [Performance] performance difference between deno_mongo and node.js driver Dec 2, 2021
@erfanium
Copy link
Member

erfanium commented Dec 2, 2021

@MierenManz
I think you were interested in performance/benchmark stuff! This can be a good start

@MierenManz
Copy link

Thanks I'll look into it :D

@jiawei397
Copy link

@yuqiang-yuan Thank you yeah, there's work to be done about performance but i try to keep everything in balance. some important features are still missing. so it would be nice if others could help about this issue

Thank you very much for your repair. Last Thursday, I also found the query difference between the Deno and nodejs versions of my project, which is much better than before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants