Performance: Pagination and sorting with Pageable
This concept affects how much data is loaded and rendered at once, impacting page load speed and responsiveness when displaying lists.
Jump into concepts and practice - no test required
Pageable pageable = PageRequest.of(page, size, Sort.by("name").ascending());
Page<Item> items = itemRepository.findAll(pageable);List<Item> items = itemRepository.findAll(); // loads all items without pagination or sorting| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load all data at once | High (many nodes) | Multiple reflows due to large DOM | High paint cost | [X] Bad |
| Use Pageable for pagination and sorting | Low (limited nodes) | Single reflow per page load | Low paint cost | [OK] Good |
Pageable in Spring Boot?Pageable is used to request data in pages, not all at once.Pageable object for page 2, size 5, sorted by "name" ascending?PageRequest.of(page, size, Sort.by("field")) for ascending sort.repository.findAll(PageRequest.of(0, 3, Sort.by("age")))Pageable pageable = PageRequest.of(1, 10, Sort.asc("date"));Sort.by(), not Sort.asc().PageRequest.of takes 3 parameters here.Pageable creation is correct?Sort.by(Order.desc("price"), Order.asc("name")) to combine sorting directions.PageRequest.of and Sort.by with orders.