Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Await vs Then
(version: 0)
Comparing performance of:
Then vs Await
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function foo(i) { return Promise.resolve(i+1); } function recur(i) { return i >= 10 ? Promise.resolve(i) : foo(i).then(recur); } function testThen() { return foo(0).then(recur); } async function testAwait() { let i = await foo(0); while (i < 10) { i = await foo(i); } return i; }
Tests:
Then
testThen()
Await
testAwait();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Then
Await
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Then
1032057.2 Ops/sec
Await
823168.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases to understand what's being compared. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares two approaches for handling asynchronous operations: `then` vs `await`. The script, stored in the `"Script Preparation Code"` section, defines two functions: 1. `foo(i)`: Returns a promise that resolves with `i+1`. 2. `recur(i)`: A recursive function that returns a promise that resolves when `i` reaches 10. It calls itself recursively until the condition is met. The benchmark creates two test functions: 1. `testThen()`: Calls `foo(0)` and then calls `recur()` on the returned promise. 2. `testAwait()`: Waits for the result of `foo(0)` using `await` and then calls `recur()` on the returned promise. **Options Compared** The benchmark compares two approaches: 1. **Then**: Uses `.then()` to chain promises together, waiting for each promise to resolve before proceeding. 2. **Await**: Uses the `await` keyword to wait for promises to resolve directly in the code. **Pros and Cons of Each Approach** **Then:** Pros: * More explicit control over promise chaining * Can be more efficient when dealing with a large number of nested promises Cons: * Can lead to callback hell (nested `.then()` calls) if not managed carefully * May result in slower performance due to the overhead of waiting for each promise to resolve **Await:** Pros: * Simplifies asynchronous code by allowing `await` to wait for promises directly * Reduces the risk of callback hell compared to using `.then()` Cons: * Can lead to slower performance if not optimized correctly (e.g., avoiding unnecessary await statements) * Requires a compatible JavaScript engine that supports the `async/await` syntax **Library and Syntax** The benchmark uses the following library: * None explicitly mentioned, but it's likely using built-in JavaScript features like Promises and async/await. No special JavaScript features or syntax are used in this benchmark. The focus is on comparing two approaches for handling asynchronous operations. **Other Alternatives** If you were to design an alternative benchmark, you might consider adding more test cases or variations, such as: * Using `async/await` with a library like `bluebird` that provides a more efficient promise implementation * Adding tests for handling errors and edge cases (e.g., promises rejected early) * Comparing the performance of different types of asynchronous operations (e.g., callbacks, promises, async functions) The provided benchmark is a good starting point for understanding the trade-offs between `then` and `await`, but you could expand on it to cover more scenarios and explore additional optimization techniques.
Related benchmarks:
Promise vs Async Await
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?