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

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

·

0 min read

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 used:

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

Sample Dataset

I used IMDB title dataset to populate my MongoDB test instance. Test Queries

I performed 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 returns 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 10 times and the query time are averaged out.

1*6glGrDH9LtdmcCIwj2lgJQ.png

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.

First published on 2019-01-15

Republished on Codeburst.io