Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fn vs async 2
(version: 0)
Comparing performance of:
f1 vs f2
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let i = 0; function f1() { return 1; } async function f2() { return 2; }
Tests:
f1
i += f1()
f2
f2().then(res => { i += res; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
f1
f2
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):
I'll explain the benchmark in detail. **Benchmark Overview** The benchmark is designed to compare the performance of two functions: `f1` and `f2`. Both functions return a value, but they do so asynchronously (`f2`) or synchronously (`f1`). The goal is to measure which approach is faster. **Script Preparation Code** The script preparation code defines two functions: * `f1`: A synchronous function that returns the value `1`. * `f2`: An asynchronous function that returns a promise, which resolves to the value `2`. The script also initializes a variable `i` with a value of `0`. **Html Preparation Code** There is no HTML preparation code provided. **Test Cases** There are two test cases: 1. **f1**: The benchmark definition is "i += f1()". This means that the function `f1` is called and its return value (`1`) is added to the variable `i`. 2. **f2**: The benchmark definition is "f2().then(res => {\r\n i += res;\r\n});". This means that the function `f2` is called, and once it returns a promise, the code inside the `.then()` block is executed, adding the resolved value (`2`) to the variable `i`. **Library: None** There are no libraries used in this benchmark. **JavaScript Feature/Syntax: Async/Await** This benchmark uses JavaScript's async/await syntax. The `async` keyword indicates that a function returns a promise, and the `await` keyword is used to pause execution until the promise is resolved. The pros of using async/await include: * Improved readability: The code looks more synchronous, making it easier to understand. * Better error handling: If an exception occurs in the promise chain, it will be propagated correctly. However, there are also some cons: * Performance overhead: Creating a new promise and resolving it can incur additional overhead. * Blocking behavior: Async code can still block the execution of other tasks if not used carefully. In this benchmark, async/await is used to demonstrate the difference in performance between synchronous and asynchronous code. However, it's worth noting that if the server or database responses were slow, using async/await would make a significant difference in performance. **Other Alternatives** To measure this kind of performance difference, you could also use other approaches, such as: * **Using Web Workers**: If your application can take advantage of multiple CPU cores, you could run `f1` and `f2` in separate web workers to compare their performance. * **Measuring CPU time**: Instead of measuring the number of executions per second, you could measure the total execution time for each function and compare the results. * **Using a profiling tool**: A tool like Chrome DevTools or Node.js Inspector can provide detailed information about CPU usage and memory allocation, helping to understand performance differences. Keep in mind that these alternatives may require more complex setup and testing to ensure accurate results.
Related benchmarks:
just promise vs just callback
async function w/microtask vs sync function
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?