Skip to content

Commit

Permalink
feat: improved performance of collection.count()
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-oloughlin committed Nov 3, 2023
1 parent f979907 commit b28f518
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,19 @@ export class Collection<
* @returns A promise that resolves to a number representing the performed count.
*/
async count(options?: CountOptions<T1>) {
// Initiate count variable, increment for each document entry, return result
// Initiate count result
let result = 0

// Perform efficient count if counting all document entries
if (selectsAll(options)) {
const iter = this.kv.list({ prefix: this._keys.idKey }, options)
for await (const _ of iter) {
result++
}
return result
}

// Perform count using many documents handler
await this.handleMany(this._keys.idKey, () => result++, options)
return result
}
Expand Down

0 comments on commit b28f518

Please sign in to comment.