2

I'm making an interceptor to log my http requests.

So far, so good, everything is working as expected.

What I want now is to get the time the request took to be executed.

I thought I could do something like this

const start = Date.now();
return next
  .handle(req)
  .map(res => {
    console.log('took ' + (Date.now() - start) + 'ms');
    return res;
  })

}

But the console shows 1 to 2ms, while the network shows more than 50ms ... I think that I should create the start value right when I create the request, but I don't know how.

Any solution ?

PS : my linting config forbids me to use console.time()

1 Answers1

6

use performance.now() to measure time duration in milliseconds

var start = performance.now(); 

return next
  .handle(req)
  .map(res => {
    console.log('took ' + (performance.now() - start) + 'ms');
    return res;
  })

For futher info check this

Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
  • Didn't know that, thank you, but it's even worse :D I just made an edit saying I should instantiate the start value right when I create the request, but since Angular is abstracting all of the process, I don't know how. Anyways, +1 for teaching me something ! –  Jan 05 '18 at 14:38
  • Did you ever resolve this time discrepancy? I am running into a similar issue, but I am seeing 300ms in the browser's network tab and 17,000ms in my logs. – Greg Kopp Jul 21 '21 at 17:46