Performance: DataLoader integration
HIGH IMPACT
This affects the speed of resolving multiple related data requests by batching and caching them, improving server response time and reducing redundant database calls.
const loader = new DataLoader(async userIds => { const posts = await this.postsService.findByUserIds(userIds); return userIds.map(id => posts.filter(post => post.userId === id)); }); async getUsers() { return Promise.all(users.map(async user => { user.posts = await loader.load(user.id); return user; })); }
async getUsers() { return Promise.all(users.map(async user => { user.posts = await this.postsService.findByUserId(user.id); return user; })); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No DataLoader (many DB calls) | N/A (server-side) | Multiple sequential DB calls delay response | Blocks initial paint until all data fetched | [X] Bad |
| With DataLoader (batched calls) | N/A (server-side) | Single batched DB call reduces delay | Faster initial paint due to quicker response | [OK] Good |