Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
then vs. async-await
(version: 0)
Comparing performance of:
then vs async-await
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
then
function one() { return new Promise((resolve, reject) => { return resolve(`hi`); }); } function khw() { one().then((message) => console.log(message)); } khw();
async-await
function one() { return new Promise((resolve, reject) => { return resolve(`hi`); }); } async function khw() { console.log(await one()); } khw();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
then
async-await
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
then
809301.6 Ops/sec
async-await
657977.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Understanding the Benchmark** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark tests two different approaches to handling promises in JavaScript: the traditional `then` method and the newer `async/await` syntax. **Test Cases** The test cases are as follows: 1. **"then"`**: This test case uses the traditional `then` method to handle a promise returned by the `one()` function. The `one()` function returns a promise that resolves with the string "hi". The `khw()` function calls `one()` and logs the result using `console.log()`. 2. **"async-await"`**: This test case uses the newer `async/await` syntax to handle a promise returned by the `one()` function. The `one()` function returns a promise that resolves with the string "hi". The `khw()` function calls `one()` and logs the result using `console.log()`, but this time using `await` instead of `.then()`. **Comparison** The benchmark compares the performance of these two approaches: * **Pros of then**: This method is widely supported by older browsers and has been the de facto standard for handling promises. It's also a good learning experience, as it helps developers understand how promises work. * **Cons of then**: The `then` method can lead to callback hell, where nested callbacks can make code harder to read and maintain. This approach can also be error-prone, especially when dealing with multiple promise chains. **Pros of async/await** * **Benefits of async/await**: This syntax is more modern and easier to read than the `then` method. It reduces the risk of callback hell and makes code more concise. * **Cons of async/await**: This syntax is only supported by newer browsers, which may lead to compatibility issues in older environments. **Library** There are no libraries used in these test cases. **Special JS Feature/Syntax** The `async` and `await` keywords are special features introduced in ECMAScript 2017 (ES7). They allow developers to write asynchronous code that's more readable and easier to maintain. The `await` keyword is a shorthand for `then()` with `.catch()`, which makes it easier to handle promises. **Other Alternatives** If you prefer the `then` method or want to support older browsers, you can use alternative approaches like: * **Promise.prototype.then()**: This method allows you to chain promises together using the `then()` method. * **Promise.prototype.catch()**: This method allows you to handle errors in promise chains using the `catch()` method. However, if you're targeting modern browsers and want to take advantage of async/await syntax, it's generally recommended.
Related benchmarks:
promise vs callback
Awaiting sync vs async 2
then vs. async-await simple
Promise vs async vs callbacks
callbacks vs promises vs async/awaits
Comments
Confirm delete:
Do you really want to delete benchmark?