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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With