0

In terms of performance and convenience which is better, using Custom Object or Map in Java to store/retrieve documents in Firestore. Does using Custom Object cause performance issues?

// HashMap
Map<String, Object> sampleMap = new HashMap<>();
sampleMap.put("name","sample1");
sampleMap.put("age", 26);

// Custom Class
class Sample {
    String name;
    int age;

    public Sample(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

// Custom Object
Sample sample = new Sample("sample1", 26);

FirestoreOptions firestoreOptions =
        FirestoreOptions.getDefaultInstance().toBuilder().setProjectId("project-id")
                .setCredentials(GoogleCredentials.getApplicationDefault()).build();
Firestore db = firestoreOptions.getService();

// using Custom Object
db.collection("SampleData").document().set(sample);

// using HashMap
db.collection("SampleData").document().set(sampleMap);

Which is better for an application with 100 reads/100 writes per second?

Does having a custom class justify the performance cost (if any)?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

I don't think there is a lot of performance costs, even if there are 100 reads/writes per second. And using custom class is definitely the way to go, otherwise you would have to use the snapshot.data object, which can cause human errors etc

0

When you pass a POJO type object to Firestore, there is a significant hit in performance, especially the first time that class is used. That's because the Firestore SDK has to use JVM reflection to serialize or deserialize the object to the document. Reflection is typically pretty slow, though it might be fast enough for your purposes. You should benchmark to know for sure.

You will get better performance if you send and receieve Maps. You will also write more code, but it will definitely run faster if you do it correctly. It's up to you to determine if you prefer convenience over performance.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441