Performance: Pagination patterns
Pagination patterns affect how quickly users see content and how much data the browser processes at once, impacting load speed and interaction responsiveness.
const page = req.query.page || 1; const limit = 20; const items = await db.getItems({ skip: (page - 1) * limit, limit }); res.json(items);
const allItems = await db.getAllItems();
res.json(allItems);| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load all data at once | High (many nodes) | Many reflows | High paint cost | [X] Bad |
| Paginate data per page | Low (few nodes) | Few reflows | Low paint cost | [OK] Good |
What is the main purpose of pagination in a Node.js application?
Which of the following is the correct way to use limit and offset for pagination in a SQL query within Node.js?
SELECT * FROM users ORDER BY id ASC LIMIT ? OFFSET ?;
Given this Node.js code snippet using a database query for pagination:
const page = 3;
const limit = 10;
const offset = (page - 1) * limit;
const results = await db.query('SELECT * FROM products ORDER BY id ASC LIMIT ? OFFSET ?', [limit, offset]);
console.log(results.length);If the database has 25 products, what will results.length print?
Identify the error in this Node.js pagination code snippet:
const page = 0;
const limit = 10;
const offset = page * limit;
const results = await db.query('SELECT * FROM items LIMIT ? OFFSET ?', [limit, offset]);What is the problem with this code?
You want to implement cursor-based pagination in Node.js for a real-time chat app. Which approach is best to fetch the next page of messages?
// Current last message ID is stored in lastMessageId
const query = 'SELECT * FROM messages WHERE id < ? ORDER BY id DESC LIMIT ?';
const results = await db.query(query, [lastMessageId, limit]);Why is this approach better than using offset?
Pass the test and this pattern moves unknown