Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

76
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda