Iterating result very slow
See original GitHub issueHi,
Here are my java classes :
public class PublicArticleEntity extends RealmObject {
/**
* For fast import no @PrimaryKey defined
*/
private String id;
private String type;
@Index
private String manufacturerId;
private Boolean sterilizable;
private Boolean active;
private Date importModified;
private RealmList<PublicArticlePartNumberEntity> partNumbers = new RealmList<>();
private RealmList<PublicArticleCodificationEntity> codifications = new RealmList<>();
public class PublicArticlePartNumberEntity extends RealmObject {
@Index
private String partNumber;
@Index
private String normalizedPartNumber;
private PublicArticleEntity publicArticle;
}
public class PublicArticleCodificationEntity extends RealmObject {
@Index
private String type;
@Index
private String value;
private PublicArticleEntity publicArticle;
}
And here is the piece of code where I have a problem. The query returns 24 results and I use the limit to get the 21 first elements. It tooks 2000ms to do the iteration 😮
RealmResults<PublicArticlePartNumberEntity> entities = realm.where(PublicArticlePartNumberEntity.class)
.equalTo("publicArticle.manufacturerId", manufacturerId.toString())
.beginGroup()
.contains("partNumber", search, Case.INSENSITIVE)
.or()
.contains("normalizedPartNumber", search, Case.INSENSITIVE)
.endGroup()
.findAllSorted("partNumber", Sort.ASCENDING);
Set<String> items = new HashSet<>();
int listSize = entities.size();
for (int i = 0; i < listSize && i < limit; i++) {
items.add(entities.get(i).getPartNumber());
}
Here the state of each :

Because Realm is using lazy loading the request if very very fast, but the for is taking 2s.
Did I miss something ?
Issue Analytics
- State:
- Created 6 years ago
- Comments:31 (25 by maintainers)
Top Results From Across the Web
Iterating through a list is very slow, how to improve the ...
This is a bad way to sort. You're effectively looping over the list 4 times, extracting specific groups of items each time.
Read more >Performance issue iterating FeatureQueryResult
I have a VB.net application that is using QueryFeaturesAsync to retrieve a FeatureQueryResult from a shapefile. It typically returns between ...
Read more >Very slow loop - Performance - Julia Programming Language
Hi all, I am trying to speed up this loop. ... The time goes to 0.2 seconds here if I comment that, and...
Read more >Slow performance when iterating over large documents takes ...
When iterating over queried cursor, getting each document takes up to 0.5s. Is that normal performance for larger files or is there ...
Read more >Why Your Loops Are So Slow | by Emmett Boudreau
Another one is O(log n), which is where there is slightly more tax, but with more iterations the loop plateaus. This is popular...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

So here is the test as @beeender requested. I’m in debug mode so it takes more time and on a production device that is waaaaaay slower than my One Plus 3T, but it’s a good thing to see differences.
First test with current code (not changed) :
Test with code
If I use
containsit’s a bit long for UX. I’ll stick withbeginWithfor now