Estoy haciendo api en el nodo js donde en una api llamo algunos datos a una api. El problema es que esto es lento, ¿hay alguna forma de hacerlo más eficiente?
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; }Estás haciendo un montón de llamadas asíncronas en serie que no parecen depender unas de otras, por lo que puedes ver una ligera mejora si las haces en paralelo:
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; } (Se garantiza que la matriz con la que Promise.all cumple su promesa estará en el orden iterable [una matriz en este caso] que recibe como entrada).
Es posible que pueda incluir la asignación de profileDetail.user allí, pero no estaba claro para mí lo que está sucediendo allí porque lo está asignando dos veces. Aún así, creo que esto hace lo mismo (suponiendo que no importa cuándo se llama a deleteSecurityProperties en relación con las llamadas findAllByUserId que lo siguen):
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; }