Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can using async-await give you any performance benefits?

Whenever I read about async-await, the use case example is always one where there's a UI that you don't want to freeze. Either all programming books/tutorials are the same or UI blocking is the only case of async-await that I should know about as a developer.

Are there any examples of how one could use async-await to eke out performance benefits in an algorithm? Like let's take any of the classic programming interview questions:

  • Find the nearest common ancestor in a binary tree
  • Given a[0], a[1], ..., a[n-1] representing digits of a base-10 number, find the next highest number that uses the same digits
  • Find the median of two sorted arrays (i.e. the median number if you were to merge them)
  • Given an array of numbers 1, 2, ..., n with one number missing, find the missing number
  • Find the largest 2 numbers in an array

Is there any way to do those using async-await with performance benefits? And if so, what if you only have 1 processor? Then doesn't your machine just divide its time between tasks rather than really doing them at the same time?

like image 268
user6048670 Avatar asked Apr 17 '16 23:04

user6048670


People also ask

What is a benefit of using async await?

A significant benefit of the async/await pattern in languages that support it is that asynchronous, non-blocking code can be written, with minimal overhead, and looking almost like traditional synchronous, blocking code.

Should I use async await everywhere?

In addition, writing async / await everywhere in your code is manual and painful. Async should be the default mode for modern javascript.

Is async await good?

The best feature of async await is making handling unhandled errors from promises much more natural to the language by enabling you to use a simple try/catch block.

Why is async await better than then?

Async/await and then() are very similar. The difference is that in an async function, JavaScript will pause the function execution until the promise settles. With then() , the rest of the function will continue to execute but JavaScript won't execute the . then() callback until the promise settles.


1 Answers

In this interview, Eric Lippert compared async await with a cook making breakfast. It helped me a lot to understand the benefits of async-await. Search somewhere in the middle for 'async-await'

Suppose a cook has to make breakfast. He has to toast some bread and boil some eggs, maybe make some tea as well?

Method 1: Synchronous. Performed by one thread. You start toasting the bread. Wait until the bread is toasted. Remove the bread. Start boiling water, wait until the water boils and insert your egg. Wait until the egg is ready and remove the egg. Start boiling water for the tea. Wait until the water is boiled and make the tea.

Youl see all the waits. while the thread is waiting it could do other things.

Method 2: Async-await, still one thread You start toasting the bread. While the bread is being toasted you start boiling water for the eggs and also for the tea. Then you start waiting. When any of the three tasks is finished you do the second part of the task, depending on which task finished first. So if the water for the eggs boils first, you cook the eggs, and again wait for any of the tasks to finish.

In this description only one person (you) is doing all the stuff. Only one thread is involved. The nice thing is, that because there is only one thread doing the stuff the code looks quite synchronous to the reader and there is not much need to make your variables thread safe.

It's easy to see that this way your breakfast will be ready in shorter time (and your bread will still be warm!). In computer life these things will happen when your thread has to wait for another process to finish, like writing a file to a disk, getting information from a database or from the internet. Those are typically the kind of functions where'll see an async version of the function: Write and WriteAsync, Read and ReadAsync.

Addition: after some remarks from other users elsewhere, and some testing, I found that in fact it can be any thread who continues your work after the await. This other thread has the same 'context', and thus can act as if it was the original thread.

Method 3: Hire cooks to toast the bread and boil the eggs while you make the tea: Real asynchronous. Several threads This is the most expensive option, because it involves creating separate threads. In the example of making breakfast, this will probably not speed up the process very much, because relatively large times of the process you are not doing anything anyway. But if for instance you also need to slice tomatoes, it might be handy to let a cook (separate thread) do this while you do the other stuff using async-await. Of course, one of the awaits you do is await for the cook to finish his slicing.

Another article that explains a lot, is Async and Await written by the ever so helpful Stephen Cleary.

like image 125
Harald Coppoolse Avatar answered Sep 23 '22 22:09

Harald Coppoolse