Egor Panok

Full Stack JavaScript Developer

The profit of using Mongoose lean() method

September 23, 2018 - 3 min read

The profit of using Mongoose lean() method

Mongoose has a really good feature for query performance optimization when you have read-only scenarios. It’s the lean() method. As the official Mongoose documentation states, documents returned from queries with the lean option enabled are plain javascript objects, not MongooseDocuments, having no save method, getters/setters or other Mongoose magic applied.

The question

So, I wondered, if there is threshold when using the lean() method is not that profitable or it’s a real silver bullet if you have read-only scenarios, i.e. reporting.

Solution - Performance Tests

To figured that out I decided to do the performance tests for both find() and lean() methods and then compare the nature of the data.

Here are the conditions for the experiment:

  1. Performance tests are done for different amount of objects to return (from 100 to 10000).
  2. Data model for the objects to return contained 6 fields (not heavy objects).
  3. Performance tests are done with 5 seconds break in between to exclude the possibility of affecting each other.


Please take a look at the resulting chart. X-axis represents the time required to complete the query. Y-axis represents the number of objects to return by the query.

Lean() method

Orange data - find() method. Blue data - lean() method

I also made a chart representing the dynamics of the following kpi: time for find() / time for lean(). And here it is:

Lean() method

Time for find() / Time for lean()

And here is the summary of the research:

  1. Time required for getting data from find() and lean() methods grows linearly depending on number of objects to return.
  2. The function representing find() method grows faster than the one for lean() method.
  3. The ratio time for find() / time for lean() is constant - it doesn’t really depend on the number of objects to return. What actually means, that on small numbers the profit of using lean() over find() could be vague, but on big ones it’s super vivid.


Going back to the question I set for this research if the lean() method is really a silver bullet for optimizing read-only scenarios, well, it actually is.

It dramatically increases the performance which is especially clear on bigger numbers.

Source code

For those who want to play around my research, you’re kindly welcomed to take a look at the repo.

Written by Egor Panok, full stack JavaScript Developer who loves building useful things. Follow him on Twitter

© 2020, Egor Panok, Full Stack JavaScript Developer