Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Awaiting sync vs async 2
(version: 1)
Comparing performance of:
Await on promise vs Await no promise
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
Await on promise
async function foo() { return 0; } async function doit() { await foo(); } doit().then(console.log);
Await no promise
function foo() { return 0; } async function doit() { await foo(); } doit().then(console.log);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Await on promise
Await no promise
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0
Browser/OS:
Firefox 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Await on promise
193929.2 Ops/sec
Await no promise
251098.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested. **What is being tested?** MeasureThat.net is testing the performance difference between two approaches: using `await` with a promise and without it, in this case, with an asynchronous function that returns a value directly (not wrapped in a promise). **Options compared** The two options being compared are: 1. **Await on promise**: This approach uses `await` to wait for the completion of a promise returned by another function (`foo() -> promise`). The promise is then resolved and the result is logged to the console. 2. **Await no promise**: This approach also uses `await`, but it's applied directly to an asynchronous function that returns a value, bypassing the need for a promise. **Pros and Cons of each approach** 1. **Await on promise** * Pros: + More explicit and readable code, as the intent of waiting for a promise is clear. + Easier to handle errors using `.catch()` (although not directly applicable here). * Cons: + May introduce additional overhead due to the need to create and manage promises. 2. **Await no promise** * Pros: + Can be faster, as it avoids the overhead of creating and managing promises. + Simplifies the code, making it more concise. * Cons: + May make the intent of waiting for a value less clear, especially in cases where errors are not properly handled. **Other considerations** When using `await` with an asynchronous function that returns a value, it's essential to consider how errors will be propagated and handled. In both examples, errors are not explicitly handled, which may lead to unexpected behavior or crashes. **Library usage** In the provided benchmark, no libraries are mentioned as being used. However, if you're using libraries like `async/await` polyfills or other utilities that provide a way to work with promises in older browsers, their performance impact should be considered when comparing these approaches. **Special JS feature/syntax** There's no special JS feature or syntax being tested here; the focus is on understanding how `await` works in different scenarios. However, if you're interested in exploring other features like async/await, promise chaining, or generators, MeasureThat.net likely has benchmarks for those as well. **Alternatives** If you're looking to compare performance between `await` with promises and without them, here are a few alternatives: * Using `then()` instead of `await`: This would simulate the behavior of `await` on a promise but without the benefits of asynchronous execution. * Using a callback function: Similar to `then()`, this approach uses a callback function to handle the result of an asynchronous operation. * Simulating synchronous execution: You could modify the benchmark to simulate synchronous execution by using `Promise.resolve()` and then applying the `await` keyword. Keep in mind that these alternatives might not accurately represent real-world scenarios, as they don't account for the nuances of asynchronous programming.
Related benchmarks:
async vs sync
sync vs promise
async vs sync 337-(839
async function w/microtask vs sync function
async function w/microtask vs sync function (with a little work)
Comments
Confirm delete:
Do you really want to delete benchmark?