Skip to content

Instantly share code, notes, and snippets.

Created January 14, 2019 14:13
What would you like to do?
Dive Into The Performance of mongoose-lean-virtuals - A Mongoose Plugin That Enables Virtuals in Lean Mode
async function runProfiler(numberOfTimes, limit) {
const mongoose = require('mongoose');
const TitleModel = require('./title.model');
const mongodb = "mongodb://root:root@localhost/imdb?authSource=admin"
mongoose.connection.once('open', function () {console.log('connected to mongoDB');});
await mongoose.connect(mongodb, {useNewUrlParser: true})
mongoose.Promise = global.Promise;
const Title = mongoose.model('title_basic', TitleModel);
async function runQuery(limit, lean) {
const start =;
await Title.find(null, null, {limit}).lean(lean).exec();
const stop =;
const took = stop - start;
console.log(`Limit: ${limit} \t Lean: ${JSON.stringify(lean)} \t Took: ${took}`);
return took;
let results = [];
for (let i = 1; i <= numberOfTimes; i++) {
const leanModeOn = await runQuery(limit, true);
const leanModeOnWithVirtuals = await runQuery(limit, {virtuals: true});
const leanModeOff = await runQuery(limit, false);
results.push( [leanModeOn, leanModeOnWithVirtuals, leanModeOff]);
const reducer = function(acc, x){return acc+x};
const leanModeOnAverage ={return x[0]}).reduce(reducer) / results.length;
const leanModeOnWithVirtualsAverage ={return x[1]}).reduce(reducer) / results.length;
const leanModeOffAverage ={return x[2]}).reduce(reducer) / results.length;
Average Runtime With Lean Mode On: ${leanModeOnAverage}
Average Runtime With Lean Mode On with Virtuals: ${leanModeOnWithVirtualsAverage}
Average Runtime With Lean Mode Off: ${leanModeOffAverage}`);
runProfiler(10, 1000000);
Copy link

Could you post your results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment