Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose (node.js module) causes high CPU usage

I'm using nodetime to analyze the high CPU usage of my node.js app. Over 30% of the CPU usage is coming from Mongoose:

enter image description here

The next biggest culprit, at a mere 5%, is the Garbage Collector.

I believe I've heard, before, that Mongoose can cause high CPU usage, and that it can be preferable to skip it and directly use the Mongo driver. Is this accurate?

Here's the "Geocode.decodeMnay" function, triggered this particular hotspot...

Geocode.prototype.decodeMany = function(strs, callback)
{
    var or = [],
        map = {},
        fields = {'woeid': 1, 'matched_queries': 1, 'latitude': 1, 'longitude': 1, 'radius': 1, 'name': 1},
        unique = [];

    strs = _.uniq(strs);
    for(var k=0; k<strs.length; k++)
        or.push({'matched_queries':strs[k].trim()});    

    this.model.find({$or: or}, fields, (function(e,matches){
        // ... excluded for brevity
    }).bind(this));
};

How else might I speed up this hotspot?

note that it is not the query taking a long time, as you can see, but rather the Mongo driver which is taking a long time to process the results (and consuming lots of CPU in the process).

like image 576
Zane Claes Avatar asked Feb 26 '13 19:02

Zane Claes


1 Answers

With Mongoose, it's important to use the lean option for queries with large result sets where you don't need anything but the plain JavaScript documents themselves. That should provide performance comparable to using the native driver directly.

For example, in the case above it would be:

this.model.find({$or: or}, fields).lean().exec(function(e, matches) {
    // ... excluded for brevity
}).bind(this));
like image 176
JohnnyHK Avatar answered Sep 21 '22 15:09

JohnnyHK