async/await 是否在部分场景下比 Promises 性能更慢?
Great question! Let’s cut to the chase: async/await is not inherently slower than raw Promises. It’s just syntactic sugar built directly on top of Promises—so under the hood, they execute the same asynchronous JavaScript engine logic. The 2x delay you’re seeing in your async/await code is almost certainly a result of how you structured the operations, not the syntax itself.
Why Your Async/Await Code Took 4000ms
Chances are, you wrote your async/await code to run operations sequentially instead of in parallel. Let’s compare the two approaches with your timeout example:
The Fast Promise Version (Parallel Execution)
This code starts both timers at the same time, so they finish after 2000ms together:
// Promise-based parallel execution const firstPromise = new Promise(resolve => { setTimeout(() => { console.log('first'); resolve(); }, 2000); }); const secondPromise = new Promise(resolve => { setTimeout(() => { console.log('second'); resolve(); }, 2000); }); Promise.all([firstPromise, secondPromise]);
The Slow Async/Await Version (Sequential Execution)
If you wrote your async/await code like this, you’re forcing the second operation to wait for the first one to finish—adding their delays together:
// Async/await sequential execution (slow by choice) async function run() { // Wait 2000ms for the first timer await new Promise(resolve => { setTimeout(() => { console.log('first'); resolve(); }, 2000); }); // Then wait another 2000ms for the second timer await new Promise(resolve => { setTimeout(() => { console.log('second'); resolve(); }, 2000); }); } run();
The Fast Async/Await Version (Parallel Execution)
Fixing this is easy—just start both Promises first, then await them together, same as the raw Promise approach:
// Async/await parallel execution (matches Promise speed) async function run() { // Start both timers immediately const firstPromise = new Promise(resolve => { setTimeout(() => { console.log('first'); resolve(); }, 2000); }); const secondPromise = new Promise(resolve => { setTimeout(() => { console.log('second'); resolve(); }, 2000); }); // Wait for both to finish (total 2000ms) await Promise.all([firstPromise, secondPromise]); } run();
Key Takeaway
Any performance difference between async/await and raw Promises is negligible—we’re talking nanoseconds in engine overhead, not seconds. The only time you’ll see a big gap is when you accidentally structure async/await code to run sequentially instead of in parallel, which is a logic choice, not a flaw in the syntax.
内容的提问来源于stack exchange,提问作者feerlay

