Asynchronous processing in JavaScript traditionally had a reputation for not being particularly fast. To make matters worse, debugging live JavaScript applications — in particular Node.js servers — is no easy task, especially when it comes to async programming. Luckily the times, they are a-changin’. This article explores how we optimized async functions and promises in V8 (and to some extent in other JavaScript engines as well), and describes how we improved the debugging experience for async code. Show
Note: If you prefer watching a presentation over reading articles, then enjoy the video below! If not, skip the video and read on. A new approach to async programmingFrom callbacks to promises to async functionsBefore promises were part of the JavaScript language, callback-based APIs were commonly used for asynchronous code, especially in Node.js. Here’s an example:
The specific pattern of using deeply-nested callbacks in this manner is commonly referred to as “callback hell”, because it makes the code less readable and hard to maintain. Luckily, now that promises are part of the JavaScript language, the same code could be written in a more elegant and maintainable manner:
Even more recently, JavaScript gained support for async functions. The above asynchronous code can now be written in a way that looks very similar to synchronous code:
With async functions, the code becomes more succinct, and the control and data flow are a lot easier to follow, despite the fact that the execution is still asynchronous. (Note that the JavaScript execution still happens in a single thread, meaning async functions don’t end up creating physical threads themselves.) From event listener callbacks to async iterationAnother asynchronous paradigm that’s especially common in Node.js is that of . Here’s an example:
This code can be a little hard to follow: the incoming data is processed in chunks that are only accessible within callbacks, and the end-of-stream signaling happens inside a callback too. It’s easy to introduce bugs here when you don’t realize that the function terminates immediately and that the actual processing has to happen in the callbacks. Fortunately, a cool new ES2018 feature called async iteration can simplify this code:
Instead of putting the logic that deals with the actual request processing into two different callbacks — the 8 and the 9 callback — we can now put everything into a single async function instead, and use the new 0 loop to iterate over the chunks asynchronously. We also added a 1 block to avoid the 2 problem.You can already use these new features in production today! Async functions are fully supported starting with Node.js 8 (V8 v6.2 / Chrome 62), and async iterators and generators are fully supported starting with Node.js 10 (V8 v6.8 / Chrome 68)! Async performance improvementsWe’ve managed to improve the performance of asynchronous code significantly between V8 v5.5 (Chrome 55 & Node.js 7) and V8 v6.8 (Chrome 68 & Node.js 10). We reached a level of performance where developers can safely use these new programming paradigms without having to worry about speed. The above chart shows the doxbee benchmark, which measures performance of promise-heavy code. Note that the charts visualize execution time, meaning lower is better. The results on the parallel benchmark, which specifically stresses the performance of 3, are even more exciting:We’ve managed to improve 4 performance by a factor of 8×.However, the above benchmarks are synthetic micro-benchmarks. The V8 team is more interested in how our optimizations affect real-world performance of actual user code. The above chart visualizes the performance of some popular HTTP middleware frameworks that make heavy use of promises and 5 functions. Note that this graph shows the number of requests/second, so unlike the previous charts, higher is better. The performance of these frameworks improved significantly between Node.js 7 (V8 v5.5) and Node.js 10 (V8 v6.8).These performance improvements are the result of three key achievements:
When we launched TurboFan in Node.js 8, that gave a huge performance boost across the board. We’ve also been working on a new garbage collector, called Orinoco, which moves garbage collection work off the main thread, and thus improves request processing significantly as well. And last but not least, there was a handy bug in Node.js 8 that caused 6 to skip microticks in some cases, resulting in better performance. The bug started out as an unintended spec violation, but it later gave us the idea for an optimization. Let’s start by explaining the buggy behavior:
The above program creates a fulfilled promise 8, and 6s its result, but also chains two handlers onto it. In which order would you expect the 0 calls to execute?Since 8 is fulfilled, you might expect it to print 2 first and then the 3s. In fact, that’s the behavior you’d get in Node.js 8:The 6 bug in Node.js 8Although this behavior seems intuitive, it’s not correct according to the specification. Node.js 10 implements the correct behavior, which is to first execute the chained handlers, and only afterwards continue with the async function. Node.js 10 no longer has the 6 bugThis “correct behavior” is arguably not immediately obvious, and was actually surprising to JavaScript developers, so it deserves some explanation. Before we dive into the magical world of promises and async functions, let’s start with some of the foundations. Tasks vs. microtasksOn a high level there are tasks and microtasks in JavaScript. Tasks handle events like I/O and timers, and execute one at a time. Microtasks implement deferred execution for 5/ 6 and promises, and execute at the end of each task. The microtask queue is always emptied before execution returns to the event loop.The difference between microtasks and tasksFor more details, check out Jake Archibald’s explanation of tasks, microtasks, queues, and schedules in the browser. The task model in Node.js is very similar. Async functionsAccording to MDN, an async function is a function which operates asynchronously using an implicit promise to return its result. Async functions are intended to make asynchronous code look like synchronous code, hiding some of the complexity of the asynchronous processing from the developer. The simplest possible async function looks like this:
When called it returns a promise, and you can get to its value like with any other promise.
You only get to the value of this promise 8 the next time microtasks are run. In other words, the above program is semantically equivalent to using 9 with the value:
The real power of async functions comes from 6 expressions, which cause the function execution to pause until a promise is resolved, and resume after fulfillment. The value of 6 is that of the fulfilled promise. Here’s an example showing what that means:
The execution of 2 gets suspended on the 6, and is later resumed when the 4 promise fulfills. This is more or less equivalent to chaining a handler onto the promise returned from 4. 0That handler contains the code following the 6 in the async function.Normally you’d pass a 7 to 6, but you can actually wait on any arbitrary JavaScript value. If the value of the expression following the 6 is not a promise, it’s converted to a promise. That means you can 0 if you feel like doing that: 1More interestingly, 6 works with any “thenable”, i.e. any object with a 2 method, even if it’s not a real promise. So you can implement funny things like an asynchronous sleep that measures the actual time spent sleeping: 2Let’s see what V8 does for 6 under the hood, following the . Here’s a simple async function 4: 3When called, it wraps the parameter 5 into a promise and suspends execution of the async function until that promise is resolved. Once that happens, execution of the function resumes and 6 gets assigned the value of the fulfilled promise. This value is then returned from the async function.async function handler() { await validateParams(); const dbResults = await dbQuery(); const results = await serviceCall(dbResults); console.log(results); return results;}6 under the hoodFirst of all, V8 marks this function as resumable, which means that execution can be suspended and later resumed (at 6 points). Then it creates the so-called 9, which is the promise that is returned when you invoke the async function, and that eventually resolves to the value produced by the async function.Comparison between a simple async function and what the engine turns it intoThen comes the interesting bit: the actual 6. First the value passed to 6 is wrapped into a promise. Then, handlers are attached to this wrapped promise to resume the function once the promise is fulfilled, and execution of the async function is suspended, returning the 9 to the caller. Once the 3 is fulfilled, execution of the async function is resumed with the value 6 from the 3, and the 9 is resolved with 6.In a nutshell, the initial steps for 8 are:
Let’s go through the individual operations step by step. Assume that the thing that is being 6ed is already a promise, which was fulfilled with the value 3. Then the engine creates a new 3 and resolves that with whatever’s being 6ed. This does deferred chaining of these promises on the next turn, expressed via what the specification calls a .Then the engine creates another so-called 7 promise. It’s called throwaway because nothing is ever chained to it — it’s completely internal to the engine. This 7 promise is then chained onto the 3, with appropriate handlers to resume the async function. This 0 operation is essentially what 1 does, behind the scenes. Finally, execution of the async function is suspended, and control returns to the caller.Execution continues in the caller, and eventually the call stack becomes empty. Then the JavaScript engine starts running the microtasks: it runs the previously scheduled , which schedules a new to chain the 3 onto the value passed to 6. Then, the engine returns to processing the microtask queue, since the microtask queue must be emptied before continuing with the main event loop.Next up is the , which fulfills the 3 with the value from the promise we’re 6ing — 3 in this case — and schedules the reaction onto the 7 promise. The engine then returns to the microtask loop again, which contains a final microtask to be processed.Now this second propagates the resolution to the 7 promise, and resumes the suspended execution of the async function, returning the value 3 from the 6.Summary of the overhead of 6Summarizing what we’ve learned, for each 6 the engine has to create two additional promises (even if the right hand side is already a promise) and it needs at least three microtask queue ticks. Who knew that a single 6 expression resulted in that much overhead?!Let’s have a look at where this overhead comes from. The first line is responsible for creating the wrapper promise. The second line immediately resolves that wrapper promise with the 6ed value 5. These two lines are responsible for one additional promise plus two out of the three microticks. That’s quite expensive if 5 is already a promise (which is the common case, since applications normally 6 on promises). In the unlikely case that a developer 6s on e.g. 3, the engine still needs to wrap it into a promise.As it turns out, there’s already a operation in the specification that only performs the wrapping when needed: This operation returns promises unchanged, and only wraps other values into promises as necessary. This way you save one of the additional promises, plus two ticks on the microtask queue, for the common case that the value passed to 6 is already a promise. This new behavior is already . For V8 v7.1, the new behavior can be enabled using the 06 flag. We’ve proposed this change to the ECMAScript specification as well.Here’s how the new and improved 6 works behind the scenes, step by step:Let’s assume again that we 6 a promise that was fulfilled with 3. Thanks to the magic of the 3 now just refers to the same promise 5, so there’s nothing to do in this step. Afterwards the engine continues exactly like before, creating the 7 promise, scheduling a to resume the async function on the next tick on the microtask queue, suspending execution of the function, and returning to the caller.Then eventually when all JavaScript execution finishes, the engine starts running the microtasks, so it executes the . This job propagates the resolution of 3 to 7, and resumes the execution of the async function, yielding 3 from the 6.Summary of the reduction in 6 overheadThis optimization avoids the need to create a wrapper promise if the value passed to 6 is already a promise, and in that case we go from a minimum of three microticks to just one microtick. This behavior is similar to what Node.js 8 does, except that now it’s no longer a bug — it’s now an optimization that is being standardized!It still feels wrong that the engine has to create this 7 promise, despite being completely internal to the engine. As it turns out, the 7 promise was only there to satisfy the API constraints of the internal 0 operation in the spec.This was recently addressed in an editorial change to the ECMAScript specification. Engines no longer need to create the 7 promise for 6 — most of the time.Comparison of 6 code before and after the optimizationsComparing 6 in Node.js 10 to the optimized 6 that’s likely going to be in Node.js 12 shows the performance impact of this change: 5/ 6 outperforms hand-written promise code now. The key takeaway here is that we significantly reduced the overhead of async functions — not just in V8, but across all JavaScript engines, by patching the spec.Update: As of V8 v7.2 and Chrome 72, 06 is enabled by default. The patch to the ECMAScript specification was merged.Improved developer experienceIn addition to performance, JavaScript developers also care about the ability to diagnose and fix problems, which is not always easy when dealing with asynchronous code. Chrome DevTools supports async stack traces, i.e. stack traces that not only include the current synchronous part of the stack, but also the asynchronous part: This is an incredibly useful feature during local development. However, this approach doesn’t really help you once the application is deployed. During post-mortem debugging, you’ll only see the 33 output in your log files, and that doesn’t tell you anything about the asynchronous parts.We’ve recently been working on zero-cost async stack traces which enrich the 33 property with async function calls. “Zero-cost” sounds exciting, doesn’t it? How can it be zero-cost, when the Chrome DevTools feature comes with major overhead? Consider this example where 4 calls 36 asynchronously, and 36 throws an exception after 6ing a promise: 4Running this code in Node.js 8 or Node.js 10 results in the following output: 5Note that although the call to 39 causes the error, 4 is not part of the stack trace at all. This makes it tricky for JavaScript developers to perform post-mortem debugging, independent of whether your code is deployed in a web application or inside of some cloud container.The interesting bit here is that the engine knows where it has to continue when 36 is done: right after the 6 in function 4. Coincidentally, that’s also the place where the function 4 was suspended. The engine can use this information to reconstruct parts of the asynchronous stack trace, namely the 6 sites. With this change, the output becomes: 6In the stack trace, the topmost function comes first, followed by the rest of the synchronous stack trace, followed by the asynchronous call to 36 in function 4. This change is implemented in V8 behind the new 48 flag. Update: As of V8 v7.3, 48 is enabled by default.However, if you compare this to the async stack trace in Chrome DevTools above, you’ll notice that the actual call site to 4 is missing from the asynchronous part of the stack trace. As mentioned before, this approach utilizes the fact that for 6 the resume and suspend locations are the same — but for regular 52 or 53 calls, this is not the case. For more background, see Mathias Bynens’s explanation on why 6 beats 52.ConclusionWe made async functions faster thanks to two significant optimizations:
On top of that, we’ve improved the developer experience via zero-cost async stack traces, which work with 6 in async functions and 3.And we also have some nice performance advice for JavaScript developers:
Posted by Maya Armyanova (@Zmayski), always-awaiting anticipator, and Benedikt Meurer (@bmeurer), professional performance promiser. Apa Itu Promise di javascript?Promise merupakan salah satu fitur yang penting dari ES6. Objek promise mewakili penyelesaian atau sebuah kesalahan pada operasi Asynchronous. Pada artikel sebelumnya kita telah belajar tentang Asynchnous pada javascript, tak lengkap rasanya apabila kita belum paham tentang Promise.
Apa itu async Await Javascript?Async/await adalah fitur yang hadir sejak ES2017. Fitur ini mempermudah kita dalam menangani proses asynchronous.Async/Await merupakan sebuah syntax khusus yang digunakan untuk menangani Promise agar penulisan code lebih efisien dan rapih.
|