Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sync vs Promise benchmark test (ver 2)
(version: 0)
Comparing performance of:
Sync vs Promise-await vs Promise-then
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function foo() { const array = []; const count = 1000000; for (let i = 0; i < count; i++) { array.push(Math.random() * count); } return array.map(num => num / 2); }
Tests:
Sync
const run = () => { const result = foo(); console.log(result); }; run();
Promise-await
const run = async () => { const promise = new Promise(resolve => { resolve(foo()); }); const result = await promise; console.log(result); }; run();
Promise-then
const run = () => { const promise = new Promise(resolve => { resolve(foo()); }); promise.then(result => { console.log(result); }); }; run();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Sync
Promise-await
Promise-then
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The MeasureThat.net website provides a JavaScript microbenchmarking tool, where users can create and run their own benchmarks or use existing ones. The provided benchmark is designed to compare the performance of three different approaches: synchronous execution, promise-return execution with await, and promise-return execution with then. **What is tested?** In this benchmark, the following aspects are being tested: * **Synchronous Execution (Sync)**: The benchmark measures the execution time of running `foo()` synchronously without using promises. * **Promise-return Execution with Await (Promise-await)**: This approach uses an async function to execute `foo()` and await its result. * **Promise-return Execution with Then (Promise-then)**: In this method, a promise is created and resolved with the result of `foo()`. The execution time of waiting for this promise to resolve is measured. **Comparison options** The benchmark compares three different approaches: 1. Synchronous Execution (Sync): This approach executes `foo()` synchronously without using promises. 2. Promise-return Execution with Await (Promise-await): This method uses an async function to execute `foo()` and await its result. 3. Promise-return Execution with Then (Promise-then): In this scenario, a promise is created and resolved with the result of `foo()`. The execution time of waiting for this promise to resolve is measured. **Pros and Cons** * **Synchronous Execution (Sync)**: * Pros: Synchronous execution can be easier to understand and debug. * Cons: It blocks the main thread, potentially causing performance issues or freezing the UI. * **Promise-return Execution with Await (Promise-await)**: * Pros: This approach allows for non-blocking code, improving responsiveness and performance. * Cons: It may require more complex code to manage promises and awaits. * **Promise-return Execution with Then (Promise-then)**: * Pros: This method is often used in older browsers that don't support async/await. * Cons: It can lead to callback hell if not handled carefully, making the code harder to read. **Library usage** The benchmark uses the `Promise` constructor and `async/await` syntax. The library being used here is a part of the JavaScript standard library. **Special JS features** * **Async/Await**: This feature allows for writing asynchronous code that looks like synchronous code, using an async function. * **Promises**: A promise is a result object that can be in one of two states: fulfilled or rejected. The `new Promise()` constructor creates a new promise. **Other alternatives** If you want to explore alternative approaches: * You could use `web workers` for asynchronous computations. * Another approach is using **WebAssembly**, which provides low-level performance and concurrency capabilities. * For more complex scenarios, consider using **ReactiveX** or **RxJS**, which provide reactive programming capabilities. In conclusion, the MeasureThat.net benchmark provides a clear comparison of three different approaches to executing asynchronous code in JavaScript. By understanding the pros and cons of each approach, developers can choose the best method for their specific use cases.
Related benchmarks:
sync vs promise
async vs sync 337-(839
Sync vs Promise benchmark test
Sync vs Promise benchmark test (ver 1)
Comments
Confirm delete:
Do you really want to delete benchmark?