-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An approach to implement async/await in Javascript with nearly zero overhead #3
Comments
I know Babel also transpiles to code using state machines to be able to support async/await in lower ECMAScript versions, but I haven't studied in detail what those state machines look like. Can you explain if this is actually different and how? |
The difference is that this approach compile one async function to two functions: one is fast like the original function but wrapped in a try catch block. the other one is compiled state machine. The async function call is rewrote with addition parameter |
Cool, that sounds like an interesting optimization indeed! |
Typescript compiled es3 javascript code:
|
my effects compiler can help to implement and experiment with different options pretty quickly https://github.com/awto/effectfuljs Avoiding promises |
Generator base approach:
|
Consider this Javascript, executed in chrome in 2775.01ms on my MBP (please close devtools first before you test it). Why? Because under the hood ES7 use promise to implement await/async, which introduce massive overhead. Call an async function is doggy slow even if it actually doesn't do any async thing.
Now, I'll present you a novel approach to implement async/await. It combines CPS, state machine and exception to reconstruct call stack and avoid closure creation.
The javascript above then can transform to the javascript below. You can test it in your chrome. I'll explain it next time. These code is just prototype for proof. You don't need handwrite your async logic like these. There will be some compilers do it. Thank you!
The text was updated successfully, but these errors were encountered: