Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure API response time with fetch in node.js

One of the functions in my node.js app is calling an external API using the fetch function. I am trying to find the most accurate way to measure the response time for the said API.

i am using the "Date" method to do it:

     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);

Is there a better\more accurate way to do it with fetch?

like image 356
Ofer B Avatar asked Mar 30 '26 23:03

Ofer B


1 Answers

You can use console.time & console.timeEnd as:

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

It will print the time elapsed like

timer1: 0.105ms
like image 109
Shivaji Mutkule Avatar answered Apr 02 '26 12:04

Shivaji Mutkule