How to get a Firestore document size?

I've published a npm package that calculates the size of a Firestore document.

Other packages like sizeof or object-sizeof that calculate the size of JS object will not give you a precise result because some primitives in Firestore have different byte value. For example boolean in Js is stored in 4 bytes, in a Firestore document it's 1 byte. Null is 0 bytes, in Firestore it's 1 byte.

Additionally to that Firestore has own unique types with fixed byte size: Geo point, Date, Reference.

Reference is a large object. Packages like sizeof will traverse through all the methods/properties of Reference, instead just doing the right thing here. Which is to sum String value of a document name + path to it + 16 bytes. Also, if Reference points to a parent doc , sizeof or object-sizeof will not detect circular reference here which might spell even bigger trouble than incorrect size.


I was looking in the Firebase reference expecting the metadata would have an attribute, but it doesn't. You can check it here.

So my next approach would be to figure the weight of the object as an approximation. The sizeOf library seems to have a reasonable API for it.

So it would be something like:

sizeof.sizeof(doc.data());

I wouldn't use the document snapshot, because it contains metadata, like if there are pending saves. On another hand overestimating could be better in some cases.

[UPDATE] Thanks to Doug Stevenson for the wonderful insight

So I was curious how much the difference would actually be, so with my clunky js I made a dirty comparison, you can see the demo here

Considering this object:

 {
  "boolean": true,
  "number": 1,
  "text": "example"
  }

And discounting the id this is the result:

| Method  | Bytes |
|---------|-------|
| FireDoc | 37    |
| sizeOf  | 64    |

So sizeOf library could be a good predictor if we want to overestimate (assuming calculations are fine and will behave more or less equal for more complex entities). But as explained in the comment, it is a rough estimation.


The calculations used to compute the size of a document is fully documented here. There is a lot of text there, so please navigate there to read it. It's not worthwhile to copy all that text here.

If you're having to manually compute the size of a document as it grows, my opinion is that you're probably not modeling your data scalably. If you have lists of data that can grow unbounded, you probably shouldn't be using a list field, and instead put that data in documents in a new collection or subcollection. There are some exceptions to this rule, but generally speaking, you should not have to worry about computing the size of a document in your client code.


For Android users who want to check the size of a document against the maximum of 1 MiB (1,048,576 bytes) quota, there is a library I have made and that can help you calculate that:

  • https://github.com/alexmamo/FirestoreDocument-Android/tree/master/firestore-document

In this way, you'll be able to always stay below the limit. The algorithm behind this library is the one that is explained in the official documentation regarding the Storage Size.