【发布时间】:2020-07-16 00:49:08
【问题描述】:
我正在尝试获取在节点获取操作中执行的异步函数的执行/响应时间,如下所示
async function getEditedData() {
var a = await fetch(`https://api.example.com/resorce_example`);
var b = await a.json();
// Some aditional treatment of the object obtained (b)
console.log("End of the asynchronous function")
}
我这样使用库perf_hooks,但是执行时间显示在之前
const hrtime = require ('perf_hooks').performance.now ;
var start = hrtime ();
getEditedData();
var end = hrtime ();
console.log (end - start);
我找到了 async_hooks 库 https://nodejs.org/api/perf_hooks.html#perf_hooks_measuring_the_duration_of_async_operations ,但我不明白它是如何工作的。我是 javascript/nodejs 的基础
【问题讨论】:
-
getEditedData().then(() => console.log(hrtime() - start)) -
await或.then().catch()。与调用任何异步函数没有什么不同。
标签: javascript node.js performance async-await measure