Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda