LearnBoost/mongoose — Issue #587: Potential way to speed up Mongoose
Author: cainiac
Created: 2011-10-31 18:19:55+00:00
State: open

The scenario:
When users connect to node, I go grab their account data from Mongo and bind it to the connection for the duration of their session.

The problem:
Each Mongoose instance incurs significant overhead in terms of memory consumption and event listeners. Creating 200 documents in under a second consumed 11MB of RAM.

The theory:
Abstract the Mongoose Schema into a Foundation that is used in Mongoose Schemas and in a new concept, Document Instances. The Document Instance is a native JS prototype that imitates the structure of a Mongoose Schema without incurring all of Mongoose's overhead. Append the Schema structure to the Document Instance prototype so that new instances of the Document Instance properly match what is expected in Mongoose. Append a save function that invokes Mongoose only during saves.

The execution:
https://gist.github.com/1328177

The result:
11MBs of RAM for 200 documents was reduced to 180KB of RAM for 200 Document Instances while preserving Mongoose saving functionality.

Postmortem:
By abstracting the Mongoose Schema into a Foundation, the structure of the document can be used in many other instance, not only for Document Instances, but also for code generation to make client-friendly versions of Mongoose Models that a browser can pull down and use.