-
Type: Bug
-
Resolution: Fixed
-
Priority: Critical - P2
-
Affects Version/s: None
-
Component/s: None
-
Environment:I'm running Node v8.9.4 on ubuntu 16.04 locally, connecting to an m0 replica set running mongod 3.4.13 in Atlas
-
Empty show more show less
I set up a quick benchmark to show that Node driver 3.x is much slower running `.find().toArray()` against Atlas. Here's how I set up my dependencies:
$ npm install mongodb@2.x + mongodb@2.2.35 added 1 package, removed 1 package and updated 1 package in 1.489s $ ln -s ~/Workspace/MongoDB/node-mongodb-native ./node_modules/mongodb3 $
Below is the script I ran with Atlas connection strings omitted:
const mongodb2 = require('mongodb'); // Via symlink const mongodb3 = require('mongodb3'); const m2str = 'mongodb://OMITTED'; const m3str = 'mongodb+srv://OMITTED'; run().catch(error => console.error(error.stack)); async function run() { const db2 = await mongodb2.MongoClient.connect(m2str); const client = await mongodb3.MongoClient.connect(m3str); const db3 = client.db('m3'); const reps = 2; let m2total = 0; for (let i = 0; i < reps; ++i) { m2total += await profile(db2); } console.log('mongodb 2.x average ms', m2total / reps); let m3total = 0; for (let i = 0; i < reps; ++i) { m3total += await profile(db3); } console.log('mongodb 3.x average ms', m3total / reps); console.log('mongodb 3 perf reduction in ms:', (m3total - m2total) / reps); } async function profile(db) { await db.collection('Test').deleteMany({}); for (let i = 0; i < 10; ++i) { await db.collection('Test').insertOne({ name: 'test' }); } let totalMS = 0; const start = Date.now(); await db.collection('Test').find().toArray(); return Date.now() - start; }
Here's the output I get:
$ node gh-6065_0.js mongodb 2.x average ms 87 mongodb 3.x average ms 330.5 mongodb 3 perf reduction in ms: 243.5 ^C $
I can consistently repro this performance degradation. I've drilled down into mongodb-core and found that the initial `cursor._find()` when running `toArray()` consumes most of the time, but I haven't been able to figure out anything more. Any ideas as to why driver 3.x is this much slower and what I can do to work around this?