Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
Mongodb node js Promise.all not workiing in parallel

I am making a date app. A user can send date requests to another user, the date requests will be stored in a different collection. While sending users profile, the server checks if user has sent date request or not.

export async function parseUser(_user: any, current_user:UserInterface): Promise<UserProfile>{
    if(!_user) return _user;
    const user: UserProfile = _user
    const now = moment(new Date());
    const birthday = moment(_user.birthday);
    const years = moment.duration(now.diff(birthday)).asYears();
    const task1 = DateRequest.findOne({request_sent_by: current_user.uid, request_sent_to: user.uid});
    const task2 = DateRequest.findOne({request_sent_by: user.uid, request_sent_to: current_user.uid});
    const [has_current_sent_date_request, has_this_user_sent_date_request] = await Promise.all([task1, task2])
    user.age = Math.floor(years);
    user.is_dating = user.dates.includes(current_user.uid);
    user.has_saved = current_user.saved_users.includes(user.uid);
    user.has_current_sent_date_request = has_current_sent_date_request?true:false
    user.has_this_user_sent_date_request = has_this_user_sent_date_request?true:false
    return user
}

This parseUser() function takes raw user data and calculates age, check if the current user is dating the requested user, checks if requested user has sent the date request to current user and also checks if current user has sent date request to requested user.

console.time("time")
const tasks = matching_users.map(async x => parseUser(x, res.locals.currentUser))
const parsed_users: UserProfile[] = await Promise.all(tasks)
console.timeEnd("time")
JSONReponse.success("success", parsed_users)

matching_users have raw user datas that does not include age, is_dating, has_sent_request

Even though I used Promise.all() the above code takes 3 seconds to complete.

Is there any optimization or better way of doing this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If your DateRequest.findOne tasks are being executed in the same worker they will effectively run sequentially. Each worker has a single thread. So, all tasks in the same worker are queued and processed one at a time. If you need them to execute in parallel you will need to ensure they are handled by different workers. This will add cross worker messaging overhead. However, it will improve performance if these tasks are long running enough.

Here are a couple articles that discuss multithreading in node.js

  • Node.js Multithreading!
  • How To Do Multithreading With Node.js

For multithreading to work your database access will also have to be thread safe. In that case you will also need to ensure that however you are reading MongoDB will be safe to handle simultaneous access from multiple threads.

  • MongoDB Concurrency
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!