'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?
Solution 1:[1]
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
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
