When your backend code is calling external APIs you may want to measure particular request time to identify bottlenecks.
The most straightforward, but incorrect, way to measure how long a request takes is to use JavaScript Date object:
However, this won't give you the actual time that request takes. The above request call is async, and you start measuring time at the time when the request was queued, not actually sent.
In order to determine how much time elapsed since sending the request, you can use the time parameter:
You can also compare results returned by both methods:
When I run it, I got the following results:
The actual time elapsed: 72
Time elapsed since queuing the request: 156
Notice that the first callback resolves after the second one(!)
The difference is almost 2x. Depending on your server-side code, this difference might be even larger, and give you incorrect hints while you are profiling your application.