I was doing some benchmarking to figure out which what was the best to structure my Firestore data to have the fastest queries. While doing so, I came across this interesting behavior.
In the example below, there are 1,000 documents in the Firestore collection "profiles". When I query for the whole collection, it takes on average 4.93 seconds. When I query for each individual document and wrap them in a Promise.all, it takes 2.65 seconds. Could someone explain why the latter query is almost twice as fast?
async function getProfiles() {
const startDate = Date.now();
// Fetch 1000 profiles with one query
const snapshot = await firestore.collection("profiles").get();
const midDate = Date.now();
const userIds = snapshot.docs.map((doc) => doc.id);
const allFetches = userIds.map((userId) =>
firestore.collection("profiles").doc(userId).get()
);
// Fetch 1000 profiles with 1000 queries
await Promise.all(allFetches);
const endDate = Date.now();
// ~4.93s
console.log((midDate - startDate) / 1000);
// ~2.65s
console.log((endDate - midDate) / 1000);
}
getProfiles();