Joan-Diego Rodriguez
Joan-Diego Rodriguez

Reputation: 2557

Node Express API slow (with Mongodb)

When querying a Mongo collection with Robomongo on my mac I get all 3,430 results in just over 100ms:

db.getCollection('profiles').find({'Uploader':/iazi/})

When querying my Node / Express / Mongoose API running locally, I have to wait over 2 full seconds to get the exact same data.

Here is my code:

server.js

router.route('/profiles').get(profile.rawList);

profile.js

exports.rawList = function(req, res) {
  var domain = '@'+req.user.email.split('@').pop();
  Profile
    .find({'Uploader': new RegExp(domain, 'i')})
    .exec(function(err, data){
      res.send(data);
    })
}

Who is the culprit for these 1,900ms difference? Is it Mongoose? Is it node? Did I write something wrong?

Additional info: the following responds in just 8ms

exports.superSimple = function(req, res) {
  res.json({foo:bar})
}

Upvotes: 1

Views: 1647

Answers (1)

piemonkey
piemonkey

Reputation: 751

I suspected Mongoose so did some digging. There's very little I could find on Mongoose performance but I did get this. It's pretty old but probably still valid as Mongoose is still doing a lot of magic for each object returned. Try doing it as a 'lean' query and you should get better performance as it just loads the data.

Upvotes: 1

Related Questions