I've noticed my API is slow. To return restaurants when searching on towns is around 4 seconds average, (even if there are only 10 results). Same search (without RegExp), direct on Mongo Atlas is a tenth of a second. Here is my javascript code. I'm using Express, Mongoose and RegExp in my js. Is there any way to speed this up pls? :
// Route to return all restaurants matching the searched name, returning all fields, using a collection that has collation//
app.route("/restaurants/:restaurantName")
.get(function(req, res) {
let partialToMatch = new RegExp(req.params.restaurantName, 'i');
Restaurant.find({
BusinessName: partialToMatch
}, null, {
limit: 25
}, function(err, foundRestaurant) {
if (foundRestaurant) {
res.send(foundRestaurant)
} else {
res.send("No restaurants matching that title was found.");
}
});
});