question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lazy Count the number of results

See original GitHub issue

Is there any way of counting the number of filtered objects from a realm js query without using JS’s .length property?

Code Sample

I am doing this:

const maleCount = realm.objects('Person').filtered('gender == "male"').length;

And I wanted to know if there is a faster way of doing so, some Realm method as to not throw away the laziness Realm provides.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
knethcommented, Mar 9, 2018

.length is implemented using functions in Realm Core (our storage/query engine), and it is a fairly cheap to compute.

If you have

let males = realm.objects('Person').filtered('gender == "male"');
let maleCount1 = males.length;
// do a lot but no changes to Person
let maleCount2 = males.length;

the second invocation of .length with not rerun the query.

The short answer is that we don’t expose any other method to compute the length of a result set.

0reactions
cristianoccazinspcommented, Jun 10, 2021

Since realm pretty much locks the entire app while it is running a query (not async), I would say the time budget is not too high. This is an operation intended to run shortly after an app start to do some cleanup and gather statistics. I guess I will need to test a real use-case and time it. But constant time sounds decent for this, as we are talking of about a few thousand items.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get count of lazy list in most efficient way?
In my VisitRepository I created a new function GetArticleIDsWithVisit(), which makes a direct sql call via db.SqlQuery, returning a Dictionary.
Read more >
Count observations by group - dplyr
count () lets you quickly count the unique values of one or more variables: df %>% count(a, b) is roughly equivalent to df...
Read more >
Lazy Sort: Counting Comparisons - jaspervdj
So, we get to the main trick I wanted to talk about: how do we benchmark this, and can we add unit tests...
Read more >
Lazy Queries - Lucee Documentation
It does not know how many records there are. When you loop you can count the records, so you know the total number...
Read more >
Python: Count the occurrences of each word in a given sentence
{'the': 2, 'jumps': 1, 'brown': 1, 'lazy': 1, 'fox': 1, 'over': 1, ... print('The number of words in this sentence is: ' +...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found