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

179
Vistas
Is there any way to write below code smartly?

I am making api in the node js where in one api i call some data into one api. The problem is that this is slow, is there any way to make it more efficient?

constructor() {
        this.userExperienceRepository = new UserExperienceRepository();
        this.userEducationRepository = new UserEducationRepository();
        this.userCertificationRepository = new UserCertificationRepository();
        this.userExpertiseRepository = new UserExpertiseRepository();
        this.userEquipmentRepository = new UserEquipmentRepository();
        this.userRepository = new UserRepository();
    }
async findAllByUserId(id) {

    let profileDetail={};
    profileDetail.user = await this.userRepository.findAllById(id);
    profileDetail.user= deleteSecurityProperties(profileDetail.user);
    profileDetail.experience = await this.userExperienceRepository.findAllByUserId(id);
    profileDetail.education = await this.userEducationRepository.findAllByUserId(id);
    profileDetail.certification = await this.userCertificationRepository.findAllByUserId(id);
    profileDetail.expertise = await this.userExpertiseRepository.findAllByUserId(id);
    profileDetail.equipment = await this.userEquipmentRepository.findAllByUserId(id);

    return profileDetail;
    
}
}
function deleteSecurityProperties(user) {
    delete user.password;
    delete user.otp;
    delete user.otpAttempts;
    delete user.otpTimestamp;
    return user;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're doing a bunch of async calls in series that don't seem to rely on one another, so you may see a slight improvement if you do them in parallel instead:

async findAllByUserId(id) {
    let profileDetail = {};
    profileDetail.user = await this.userRepository.findAllById(id);
    profileDetail.user = deleteSecurityProperties(profileDetail.user);
    [
        profileDetail.experience,
        profileDetail.education,
        profileDetail.certification,
        profileDetail.expertise,
        profileDetail.equipment,
    ] = await Promise.all([
        this.userExperienceRepository.findAllByUserId(id),
        this.userEducationRepository.findAllByUserId(id),
        this.userCertificationRepository.findAllByUserId(id),
        this.userExpertiseRepository.findAllByUserId(id),
        this.userEquipmentRepository.findAllByUserId(id),
    ]);

    return profileDetail;
}

(The array that Promise.all fulfills its promise with is guaranteed to be in the order of the iterable [an array in this case] it receives as input.)

You might be able to include the profileDetail.user assignment in there, but it wasn't clear to me what's going on there because you're assigning to it twice. Still, I think this does the same thing (assuming it doesn't matter when deleteSecurityProperties is called relative to the findAllByUserId calls that follow it):

async findAllByUserId(id) {
    let profileDetail = {};
    [
        profileDetail.user,
        profileDetail.experience,
        profileDetail.education,
        profileDetail.certification,
        profileDetail.expertise,
        profileDetail.equipment,
    ] = await Promise.all([
        this.userRepository.findAllById(id)).then(deleteSecurityProperties),
        this.userExperienceRepository.findAllByUserId(id),
        this.userEducationRepository.findAllByUserId(id),
        this.userCertificationRepository.findAllByUserId(id),
        this.userExpertiseRepository.findAllByUserId(id),
        this.userEquipmentRepository.findAllByUserId(id),
    ]);

    return profileDetail;
}
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