Reputation: 1762
When retrieving a document from Firestore database, you are required to call the data() method of such document to retrieve the payload.
I wonder if there are any computational costs of doing so that would require your code to be written to call it only once like this:
db.collection('documents').doc('0001').get()
.then(doc => {
const data = doc.data();
let a = data.a;
let b = data.b;
let c = data.c;
});
instead of this:
db.collection('documents').doc('0001').get()
.then(doc => {
let a = doc.data().a;
let b = doc.data().b;
let c = doc.data().c;
});
Thanks!
Upvotes: 0
Views: 79
Reputation: 317372
Strictly speaking, if the code works for you, there is not a "requirement" to change anything. Don't prematurely optimize if you don't need to. That said, it's obviously less efficient to call a method three times if just one call would suffice.
If you want to see what data() does, you can look directly at the source code. You can see that it's building a new object with each call, and populates that with the fields from the document. If you're fine with 3x the memory and compute costs than minimally required, then there's no problem.
Upvotes: 3
Reputation: 83058
I don't think there is a difference in terms of computational cost: after the promise has resolved (so when you are in the then) you have got a "doc" object and you are disconnected from the database. The two approaches you mention to get a, b and c are very similar in terms of computational cost (the difference is neglectable).
Upvotes: 1