Fix the Memory Leak and Logical Bugs in Async Data Processor
Identify and correct bugs in an asynchronous JavaScript function that processes a large dataset with complex filtering and transformations. The current implementation suffers from logical errors and potential memory leaks causing performance degradation.
Challenge prompt
You are given a JavaScript async function intended to fetch and process a large dataset in batches, filtering data based on specific criteria and transforming it before returning the final result. The current implementation has several bugs that lead to incorrect outputs and inefficient memory usage. Your task is to analyze the code, fix the bugs, optimize the handling of asynchronous batch processing, and ensure the function returns the correct processed data without causing memory leaks or performance issues.
Guidance
- • Review the async batch processing logic to ensure proper sequential execution.
- • Check for incorrect usage of array methods such as filter and map, especially within async callbacks.
- • Identify any unnecessary retention of large data structures that may cause memory leaks.
- • Validate the final output matches the expected filtered and transformed data.
Hints
- • Ensure that asynchronous loops don’t run in a way that causes data to accumulate unnecessarily in memory.
- • Remember that array methods like filter and map do not automatically handle Promises correctly.
- • Look out for variables declared in the wrong scope or accumulating data from previous batches unexpectedly.
Starter code
async function processLargeDataset(fetchBatch, batchSize, maxBatches) {
let results = [];
let batchNumber = 0;
while (batchNumber < maxBatches) {
fetchBatch(batchNumber, batchSize).then(batch => {
const filtered = batch.filter(item => item.active = true);
const transformed = filtered.map(async item => {
item.processedValue = await expensiveAsyncTransformation(item.value);
return item;
});
results = results.concat(transformed);
batchNumber++;
});
}
return results;
}
async function expensiveAsyncTransformation(value) {
// Simulate an expensive async operation
return new Promise(resolve => setTimeout(() => resolve(value * 2), 50));
}
Expected output
[{ id: 1, value: 10, active: true, processedValue: 20 }, { id: 3, value: 30, active: true, processedValue: 60 }, ...]
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.