Skip to content

Instantly share code, notes, and snippets.

@melvinkcx
Created January 14, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melvinkcx/738a00dd88e88dce1a008a78f89a06b8 to your computer and use it in GitHub Desktop.
Save melvinkcx/738a00dd88e88dce1a008a78f89a06b8 to your computer and use it in GitHub Desktop.
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 = Date.now();
await Title.find(null, null, {limit}).lean(lean).exec();
const stop = Date.now();
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 = results.map(function(x){return x[0]}).reduce(reducer) / results.length;
const leanModeOnWithVirtualsAverage = results.map(function(x){return x[1]}).reduce(reducer) / results.length;
const leanModeOffAverage = results.map(function(x){return x[2]}).reduce(reducer) / results.length;
console.log(`
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);
@jeremylcarter
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