2

我的 node.js 应用程序中的一个功能是使用 fetch 函数调用外部 API。我正在尝试找到最准确的方法来测量所述 API 的响应时间。

我正在使用“日期”方法来做到这一点:

     let start_time = new Date().getTime();
      fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then((res) => {
    return res.json();
  })
.then((data) => {
  console.log('Response time:', new Date().getTime() - start_time);

有没有更好\更准确的方法来获取?

4

1 回答 1

4

您可以使用 console.time & console.timeEnd 作为:

    console.time("timer1");
      fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then((res) => {
    return res.json();
  })
.then((data) => {
  console.timeEnd("timer1");

它会像打印过去的时间一样

timer1: 0.105ms
于 2020-04-20T12:22:57.517 回答