Performance: Sequential vs parallel async execution
This concept affects how fast asynchronous tasks complete and how responsive the application feels during execution.
Jump into concepts and practice - no test required
async function fetchParallel() { const promise1 = fetchData1(); const promise2 = fetchData2(); const promise3 = fetchData3(); const results = await Promise.all([promise1, promise2, promise3]); return results; }
async function fetchSequential() { const data1 = await fetchData1(); const data2 = await fetchData2(); const data3 = await fetchData3(); return [data1, data2, data3]; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Sequential async | Minimal DOM ops until all data fetched | 1 reflow after all data ready | Single paint after data update | [X] Bad |
| Parallel async | Minimal DOM ops, data ready sooner | 1 reflow after longest fetch completes | Single paint after data update | [OK] Good |
task1() and task2() in parallel using Promise.all?await Promise.all([task1(), task2()]) to run both tasks in parallel and wait for both.async function run() {
const result1 = await task1();
const result2 = await task2();
return [result1, result2];
}
run().then(console.log);async function run() {
const results = await Promise.all(task1(), task2());
console.log(results);
}Promise.all([task1(), task2()]) with square brackets to group promises.taskA(), taskB(), and taskC(). You want to run taskA and taskB in parallel, then run taskC only after both finish. Which code correctly implements this?await Promise.all([taskA(), taskB()]) runs both tasks at the same time and waits for both to finish.await taskC() runs taskC sequentially, ensuring it starts only after taskA and taskB complete.