Your question is Refactor for Readability and Performance. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
This function at Booking Holdings fetches hotel offers for a shortlist of candidate hotel IDs, then filters them by price and rating before returning results to the search results page. It's noticeably slow on searches with a large candidate list, and it occasionally crashes the whole search request instead of just excluding one bad result.
async function getFilteredOffers(hotelIds, maxPrice, minRating) {
let offers = [];
for (const id of hotelIds) {
const offer = await fetchOfferFromSupplier(id);
offers.push(offer);
}
let filtered = [];
for (let i = 0; i < offers.length; i++) {
if (offers[i].price <= maxPrice) {
if (offers[i].rating >= minRating) {
filtered.push(offers[i]);
}
}
}
filtered.sort((a, b) => {
if (a.price > b.price) return 1;
if (a.price < b.price) return -1;
});
return filtered;
}
Refactor this to improve both readability and performance. Explain what's actually causing the slowness and the crashes, since both are hurting a live search page.