Imagine you have multiple requests for user data by ID coming in quickly. How does DataLoader handle these requests?
Think about how batching reduces the number of database calls.
DataLoader collects all requests made during the same event loop tick and sends them as a single batch to the database, improving efficiency.
Given this DataLoader usage, what will be the value of count after all calls?
let count = 0; const loader = new DataLoader(async keys => { count += 1; return keys.map(key => ({ id: key, name: `User${key}` })); }); await Promise.all([ loader.load(1), loader.load(1), loader.load(2) ]); // What is count?
Consider how caching affects repeated keys.
DataLoader caches results for keys. The batch function runs once for keys [1,2]. The second load(1) uses the cache, so count increments only once.
Which option contains a syntax error in the batch loading function?
const loader = new DataLoader(async keys => { return keys.map(key => { id: key, name: `User${key}` }); });
Check how arrow functions return objects.
The map callback uses braces without a return statement, so it returns undefined. Wrapping the object in parentheses or adding a return fixes this.
You have a DataLoader caching millions of keys. What is the best way to prevent memory issues?
Think about controlling cache size to avoid memory overflow.
Using a custom cache with size limits and eviction prevents memory bloat while keeping caching benefits.
Given this batch function, why might the results not match the order of keys?
const loader = new DataLoader(async keys => { const users = await database.getUsersByIds(keys); return users; });
Consider how the database returns results compared to requested keys.
DataLoader expects the batch function to return results in the same order as keys. If the database returns unordered results, the batch function must reorder them accordingly.