为什么这个循环在 then 回调中比在异步函数中更快?
Why is this loop in a then callback faster than in an async function?
当我出于学习目的使用 TS 时,我发现 then 回调比使用 await 的异步函数花费更少。这是我的代码片段:
代码 async/await:
const asyncCodeBlocker = async function(){
console.log("notAsync")
await Promise.resolve();
console.time("async");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("async");
return 'billion loops done with async';
}
asyncCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsync
mainStack
async: 2.464s
billion loops done with async
带有嵌套承诺的代码:
const promiseCodeBlocker = function(){
console.log("notAsyncInPromise")
return Promise.resolve().then(() => {
console.time("promise");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("promise");
return 'billion loops done with nested promise';
})
}
promiseCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsyncInPromise
mainStack
promise: 497.627ms
billion loops done with nested promise
为什么会这样?
原始代码是用 TypeScript 编写的,但在社区成员用 JS 发布问题后对其进行了编辑和覆盖,以便能够 运行 片段。
事实证明,该行为是由我的 TS 配置引起的。我将 编译器 选项 "target" 设置为 es5。将其值更改为 es6 后,我设法为两个片段获得了相同的结果。
原始代码和结果:
代码 async/await:
const asyncCodeBlocker: () => Promise<any> = async function(){
console.log("notAsync")
await Promise.resolve();
console.time("async");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("async");
return 'billion loops done with async';
}
asyncCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsync
mainStack
async: 2.464s
billion loops done with async
带有嵌套承诺的代码:
const promiseCodeBlocker: () => Promise<any> = function(){
console.log("notAsyncInPromise")
return Promise.resolve().then(() => {
console.time("promise");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("promise");
return 'billion loops done with nested promise';
})
}
promiseCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsyncInPromise
mainStack
promise: 497.627ms
billion loops done with nested promise
当我出于学习目的使用 TS 时,我发现 then 回调比使用 await 的异步函数花费更少。这是我的代码片段:
代码 async/await:
const asyncCodeBlocker = async function(){
console.log("notAsync")
await Promise.resolve();
console.time("async");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("async");
return 'billion loops done with async';
}
asyncCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsync
mainStack
async: 2.464s
billion loops done with async
带有嵌套承诺的代码:
const promiseCodeBlocker = function(){
console.log("notAsyncInPromise")
return Promise.resolve().then(() => {
console.time("promise");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("promise");
return 'billion loops done with nested promise';
})
}
promiseCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsyncInPromise
mainStack
promise: 497.627ms
billion loops done with nested promise
为什么会这样?
原始代码是用 TypeScript 编写的,但在社区成员用 JS 发布问题后对其进行了编辑和覆盖,以便能够 运行 片段。
事实证明,该行为是由我的 TS 配置引起的。我将 编译器 选项 "target" 设置为 es5。将其值更改为 es6 后,我设法为两个片段获得了相同的结果。
原始代码和结果:
代码 async/await:
const asyncCodeBlocker: () => Promise<any> = async function(){
console.log("notAsync")
await Promise.resolve();
console.time("async");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("async");
return 'billion loops done with async';
}
asyncCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsync
mainStack
async: 2.464s
billion loops done with async
带有嵌套承诺的代码:
const promiseCodeBlocker: () => Promise<any> = function(){
console.log("notAsyncInPromise")
return Promise.resolve().then(() => {
console.time("promise");
let i = 0;
while (i < 1000000000) { i++; }
console.timeEnd("promise");
return 'billion loops done with nested promise';
})
}
promiseCodeBlocker().then(v => {console.log(v)})
console.log("mainStack")
结果:
notAsyncInPromise
mainStack
promise: 497.627ms
billion loops done with nested promise