0

I have a function, quite deep in the call stack, that may on rare occasions need to go and fetch an external resource via an HTTP request. What is the performance impact of making the function (and everything on its call stack) async, so that it is allowed to do an await?

I'm concerned particularly with the effect on calls that don't execute the await, so they actually run synchronously.

This applies to Javascript both on the browser and in Node.js

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Why are you concerned about performance if that function will on some occasions take a long time (to make an http request) anyway? – Bergi Jul 01 '20 at 09:48
  • I'm concerned whether there is an impact on the normal case, where it doesn't have to make an HTTP request. – Michael Kay Jul 01 '20 at 10:04
  • Yes, but that impact is totally negligible when compared to making the HTTP request, so you'll be fine. The find about the precise quantitative impact, you will have to benchmark your actual code, that's nothing we can help you with. – Bergi Jul 01 '20 at 10:06
  • Let me give you an example: suppose the HTTP request is only made if the user wants to buy a really unusual product, and we give them a message saying they have to wait while we fetch the data. Then its the impact on all the other paths that we care about. And if it's negligible, that's good news, but I'd like to be able to quantify that. – Michael Kay Jul 01 '20 at 10:10
  • And yes, I know I could benchmark it, but it would be an expensive experiment to conduct, so I thought I would ask first to see if anyone can give me a ballpark. – Michael Kay Jul 01 '20 at 10:13
  • "*we give them a message saying they have to wait*" - that's quite different from fetching the data and just waiting for it. Are you saying that the code that's deep in the call stack pops up a message to the user without its callers knowing? If you really want to distinguish between those very different cases, you might want to reconsider your design. – Bergi Jul 01 '20 at 10:15
  • Ballpark? <<1ms unless your callstack is *very* deeply nested :-) – Bergi Jul 01 '20 at 10:16
  • And note, we're talking about functions that usually execute in microseconds and might be called millions of times; if the execution time of the function doubles, that matters. – Michael Kay Jul 01 '20 at 10:18
  • Thanks. The call stack is indeed very deeply nested, and 1ms would be quite unacceptable. – Michael Kay Jul 01 '20 at 10:18
  • Can you give me a number of what you mean by "very deeply nested"? Also, if this function is called millions of times, and it does http requests in only 0.01% of cases on average, that's still about 100 requests that will take many milliseconds. – Bergi Jul 01 '20 at 10:22
  • You might want to have a look at https://stackoverflow.com/q/46900782/1048572 and https://v8.dev/blog/fast-async – Bergi Jul 01 '20 at 10:57
  • Thanks for the link. I will mark this Q as a duplicate. – Michael Kay Jul 01 '20 at 11:16
  • I'm not sure it's a duplicate, but if you think so I can close it. Take into account that the benchmark in that answer is over 3 years old, and your load is probably quite different. – Bergi Jul 01 '20 at 11:40

0 Answers0