In the code below
When func1 uses Promise.all, the execution time is normal.
func2’s execution time doubles when Promise.all is used.
I want to know why the time is doubled.
I checked the results in Mac, Chrome browser.
code
const func1 = ()=>{
return new Promise((resolve) => {
setTimeout(() => {
resolve("Task completed!");
}, 1000);
});
}
const func2 = async ()=>{
await 1 // This code prevents js optimization in Chrome.
for (let i = 0; i < 1e8; i++);
return "Task completed!";
}
// Measures function execution time
const funcTimer = async (name, func)=>{
console.time(name)
await func()
console.timeEnd(name)
}
await funcTimer('func1 x1 time',func1)
await funcTimer('func2 x1 time',func2)
await funcTimer('func1 x2 Promise.all time',async ()=>{
await Promise.all([
func1(),
func1()
])
})
await funcTimer('func2 x2 Promise.all time',async ()=>{
await Promise.all([
func2(),
func2()
])
})
result