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

174
Views
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 answers
Answer question

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 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!