How to calculate the execution time of an async function in JavaScript?

前端 未结 4 644
失恋的感觉
失恋的感觉 2021-01-11 23:01

I would like to calculate how long an async function (async/await) is taking in JavaScript.

One could do:

const asyncFunc =         


        
4条回答
  •  礼貌的吻别
    2021-01-11 23:25

    Any already queued microtask will be fired first, and their execution time will be taken into account.

    Yes, and there's no way around that. If you don't want to have other tasks contribute to your measurement, don't queue any. That's the only solution.

    This is not a problem of promises (or async functions) or of the microtask queue specifically, it's a problem shared by all asynchronous things which run callbacks on a task queue.

提交回复
热议问题