0

I m using a synchronous call from Express NodeJS to execute my queries in MongoDB using async await. Does this has any adverse performance impacts on the incoming request or any kind of bottleneck that can occur ? So far I have tried multiple request and it works fine. I wanted to know if the incoming number of requests increases significantly, will this result in any adverse of performance issues ?

here is the code how I have setup the MongoDb communication

export const queryToDB = async(queryHandler) =>  {
let resultSet;
    await MongoClient.connect(mongodburl, (err, client) => {
        if(err) throw Error("Database connection cannot be established.");
        resultSet = queryHandler(client.db("db_name));
    });
return resultSet
}

queryHandler example is as follows:

return connection => 
      {connection.collection('table_name').insert(value, err => {
            if (err) throw new Error(err);
            doSomething();
        });
Pranay Tripathi
  • 1,614
  • 1
  • 16
  • 24
  • Using async/await is still asynchronous. I think you'd need to show your code if you still have concerns. – JohnnyHK Nov 03 '18 at 22:58
  • The biggest "performance" issue that can immediately be seen here is that you are effectively connecting to the database on **every** invocation of this function. "Don't do that!". Database connections need to persist for the lifecycle of your application. See ["How to properly reuse connection to Mongodb across NodeJs application and modules"](https://stackoverflow.com/q/24621940/2313887) for a summary of the many workable approaches. – Neil Lunn Nov 03 '18 at 23:04
  • @JohnnyHK I have added the code. It will await the query to resolve and get the result set. – Pranay Tripathi Nov 03 '18 at 23:05
  • @NeilLunn Probably extracting it to a singleton pattern would help then – Pranay Tripathi Nov 03 '18 at 23:07
  • More like "definately". As to anything else, as noted the actual actions of `queryHandler` are completely absent from your question. Therefore nobody can yet advise if any of "that" can also be done better. – Neil Lunn Nov 03 '18 at 23:12
  • @NeilLunn added queryHandler example – Pranay Tripathi Nov 04 '18 at 09:01

0 Answers0