[Performance] Count vs Items.length

Hey There :wave:

Our question regards the use of count for responses that return many items.

We are currently optimizing our queries.

We would like to better understand the advantage of leveraging count instead of using items { id } and accessing its length using items.length.

Is there a real benefit from using the count feature? When does it really make a difference?

Thanks for your help!

Regards,
Andréas

The benefit of using count is that then your response is ONLY returning a count number vs. transferring (potentially) a large JSON response. If you have a table with 10,000,000 records that you want to know how many records are in, items.length isn’t going to work!

Count is returning the number of records visible to the query. NOT necessarily the number of records in the response. For example, if I’m querying a table with 12 records and asking for the first result.

query {
  projectsList(first: 1) {
    count
    items {
      id
    }
  }
}

Would return:

{
  "data": {
    "projectsList": {
      "count": 12,
      "items": [
        {
          "id": "ckko3p9be00uo07mrgz2809km"
        }
      ]
    }
  }
}

Meanwhile, the Count is the same as the number of records returned by a Filter as it is the number counted in a Group and Aggregation.

query {
  projectsList(
  filter: {
    status: {
      equals: "NEW"
    }
  }
  groupBy: {
    query: {
      status: {
        as: "NewProjects"
        fn: {
          aggregate: COUNT
        }
      }
    }
  }
  ) {
    count
    groups {
      NewProjects: Int
    }
  }
}

Response:

{
  "data": {
    "projectsList": {
      "count": 9,
      "groups": [
        {
          "NewProjects": 9
        }
      ]
    }
  }
}