codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Dive Into The Performance of mongoose-lean-virtuals — A Mongoose Plugin That Enables Virtuals in Lean Mode.

Melvin Koh
codeburst
Published in
3 min readJan 14, 2019

adopted from animals.sandiegozoo.org

In Mongoose, lean is enabled by default to provide better performance. Despite the performance, we loss all useful Mongoose magic methods, getter and setter. A particular magic method which I leverage all the time is virtual . To make avail of virtual in lean mode, there is an official plugin — mongoose-lean-virtuals. Since lean mode is enabled to enhance performance, I wonder how this plugin would impact the performance. Thus, I created a simple profiler to discern the performance implication of using such plugin.

(optional) A Quick Primer

This section is also available in my another article.

What is Lean mode in Mongoose?

When lean mode is enabled, all queries return plain JavaScript objects instead of Mongoose Document. In this case, Mongoose will never have to instantiate Mongoose Document from plain JS object, apply magic methods, etc. Neglecting all these overhead in Lean mode, it makes perfect sense to have lean enabled by default.

What is Virtual?

Virtuals are attributes derived during query, which the values are not actually persisted in MongoDB. Since virtual is one of Mongoose magic attributes, it is disabled by default.

Profiling Impact of mongoose-lean-virtuals

mongoose-lean-virtuals preserves lean mode while keeping virtuals enabled. This provokes my interest about its performance implication. I am curious to know how this plugin would impact the performance. Thus, I prepared a simple profiler.

Test Environment

In my test environment, I use:

  • MongoDB 4.0.5 with Docker Compose
  • Mongoose 5.4.3
  • mongoose-lean-virtuals 0.3.4
  • Node 8.15.0

Sample Dataset

I use IMDB title dataset to populate my MongoDB test instance.

Test Queries

I perform a series of find() requests on MongoDB in different modes:

  • Lean mode on
  • Lean mode on with virtual enabled
  • Lean mode off

Test Schema and Virtual

The schema used in the test is modeled after the basic title of IMDB movies.

Mongoose Schema Of IMDB Movie Title

In the schema, I added a virtual attribute isLong which return true if the run-time is longer than 30 mins. This is a fairly simple virtual, it should not be adding too much of computational load in our queries.

Test Script

Once the data is ready, I run the script which perform the aforementioned queries repeatedly and gather the results by averaging them.

The snippet is the script I ran to measure the query time.

Replicating The Test

The steps to replicate the test are written in my Github repo — mongoose_lean_virtuals_profiler. It also comes with docker-compose.yml I used. Feel free to try it on your own.

Results

The test ran for 10x and the query time are averaged out.

Average Query Time In Different Lean Modes In Milliseconds

From the screenshot, without Lean mode, the average find() time is ~2.75 mins. Whereas with lean mode, average query time are approximately ~1.5 mins without virtual and ~1.6 mins with virtual.

Disclaimer

This test is not conducted in a strict environment. The purpose of this test is merely to identify the presence of any significant impact on query time when mongoose-lean-virtuals is applied.

Summary

Apparently, given your virtual methods doesn’t perform any computational-intense operation, the impact of using mongoose-lean-virtuals is fairly acceptable. With the results, I am confident that using this plugin would not introduce significant performance issue in my feathers.js apps.

Your claps will definitely drive me further.
Feel free to give me some 👏 👏 👏.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Melvin Koh

Software Engineer | Maintainer of fastapi-events

No responses yet

Write a response