How to measure the execution time of a promise?

前端 未结 4 1858
迷失自我
迷失自我 2020-12-03 15:05

I\'m trying to write a function that measures the execution time of another function:

export class Profiler {
    public measureSyncFunc(fn: () => any): P         


        
4条回答
  •  广开言路
    2020-12-03 15:39

    Have a look at timeFnPromise and the related test cases.

    • target function is wrapped and executed when the wrapped function is called
    • appends fulfillment / rejection handler to the underlying Promise that returns the target functions return value as "ret" and the elapsed time as "elapsedTime"
    • supports arguments by passing them through to the target function

    Samples Usage:

    const wrappedFn = timeFnPromise(aFunctionThatReturnsAPromise)
    
    wrappedFn()
    .then((values)=>{
      const {ret, elapsedTime} = values
      console.log(`ret:[${ret}] elapsedTime:[${elapsedTime}]`)
    })
    

    Also available via NPM module jschest.

提交回复
热议问题