Nicolas
Nicolas•2mo ago

Concurrent fetch timing

Hey all :playful_deno: I'm trying to find a way of accurately timing a few fetch calls that are triggered concurrently. I tried something basic, but each new request will have an increased time, probably due to the internal fetch pool having some delay to send new requests:
functionCalledConcurrentlyManyTimes() {
// start timer
fetch(url).then((response) => {
// end timer
}
}
functionCalledConcurrentlyManyTimes() {
// start timer
fetch(url).then((response) => {
// end timer
}
}
I read all pages of doc and GitHub issues I could find on the subject, the node:perf_hooks API does not seem to implement resource watching, and I'm out of idea to make that work 🤔 Any help is appreciated! Cheers.
4 Replies
Doctor 🤖
Doctor 🤖•2mo ago
JavaScript can only do one thing at a time. Since you're working with promises, the more stuff you have in the event loop the more stuff it needs to get through before it returns to this task The actual fetching of the data is handled outside of the JavaScript context so can happen in parallel but making the request and handling the response happens in JavaScript and therefore only one is happening at a given time while the others sit in a queue waiting You can try console.profile('some name) and .profileEnd. But that might not work and I'm not sure exactly what it would spit out if it did work
Nicolas
NicolasOP•2mo ago
Thank you @Doctor 🤖, I wanted to implement a resource PerformanceObserver, but it's not yet available https://github.com/denoland/deno/blob/80098bfeabcd2f681f1854c6294ad0fb71c2dd06/ext/node/polyfills/perf_hooks.ts#L15. I understood that the requests are handled outside of JavaScript and thought that there might be a way to get the real execution time from some low level API. That's my first Deno script and I don't do much backend JS so any idea to improve this is welcome. I would like to store the timing information, sadly console.profile does not help for that
GitHub
deno/ext/node/polyfills/perf_hooks.ts at 80098bfeabcd2f681f1854c629...
A modern runtime for JavaScript and TypeScript. Contribute to denoland/deno development by creating an account on GitHub.
Doctor 🤖
Doctor 🤖•2mo ago
Deno v2.1 seems to be coming with a Deno.tracing API which I'd imagine would offer what you're looking for. For the perf hooks, there is performance.now() available under the WebAPIs
Doctor 🤖
Doctor 🤖•2mo ago
MDN Web Docs
Performance - Web APIs | MDN
The Performance interface provides access to performance-related information for the current page.

Did you find this page helpful?