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;
}
CodePudding user response:
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;
}