Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
functions that uses an async function in Array.find() 2
(version: 1)
functions that uses an async function in Array.find()
Comparing performance of:
async function 1 vs async function 4 vs async function 9
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
async function findAsync(arr, predicate) { const results = await Promise.all(arr.map(predicate)); return arr[results.indexOf(true)]; } async function findAsync4(arr, predicate) { let Promises = arr.map((item) => new Promise((resolve) => { if (predicate(item)) { resolve(item); } })); return new Promise((resolve, reject) => { Promises.map(p => { p.then(result => { if (result) { resolve(result); return; } }); }); }); } async function findAsync9(arr, predicate) { let Promises = arr.map((item) => new Promise((resolve) => { if (predicate(item)) { resolve(item); } })); return await Promise.any(Promises); } var items = [...Array(100000).keys()].map((e) => { return { id: e, }; })
Tests:
async function 1
(async () => { let f = await findAsync(items, (e) => e.id === 95000); })();
async function 4
(async () => { let f = await findAsync4(items, (e) => e.id === 95000); })();
async function 9
(async () => { let f = await findAsync9(items, (e) => e.id === 95000); })();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
async function 1
async function 4
async function 9
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
async function 1
4.6 Ops/sec
async function 4
8.0 Ops/sec
async function 9
7.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Definition JSON** The benchmark defines three functions: `findAsync`, `findAsync4`, and `findAsync9`. These functions are designed to find an item in an array using a predicate function. The main difference between these functions is how they handle the asynchronous nature of the search. 1. `findAsync`: Uses `Promise.all` to parallelize the search, waiting for all promises to resolve before finding the first match. 2. `findAsync4`: Manually manages promises using `new Promise` and `map`, which can lead to more control but also increases complexity. 3. `findAsync9`: Utilizes `Promise.any`, which returns as soon as one promise resolves, potentially skipping over matches. **Test Cases** Each test case runs one of the three functions with a specific input array and predicate function. The predicate function is `(e) => e.id === 95000`, searching for an item with id 95000. **Library: `Promise`** Both `findAsync4` and `findAsync9` use manual promise management, but `Promise.all` in `findAsync` provides a more concise and efficient way to handle asynchronous operations. This is because `Promise.all` allows the browser to utilize its internal caching mechanisms, reducing overhead. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used beyond ES6's promise API. **Pros and Cons of Each Approach** 1. **findAsync**: Pros: * More concise and efficient * Utilizes browser caching for Promise.all Cons: * Less control over promise management 2. **findAsync4**: Pros: * Provides more control over promise management Cons: * Increased complexity * May incur additional overhead due to manual promise handling 3. **findAsync9**: Pros: * Simplified logic Cons: * Potentially skips over matches if any promise resolves before finding the first match **Alternatives** For benchmarking purposes, alternatives like `for` loops or using external libraries for asynchronous iteration (e.g., `async-map`) can be considered. However, these may not provide a direct comparison to the native `Promise` API. In summary, the benchmark tests three functions with different approaches to handling asynchronous operations in an array search. While each function has its pros and cons, `findAsync` is likely the most efficient due to its use of `Promise.all`.
Related benchmarks:
Promise vs Async Await
async for vs promise.all vs for await
Promise vs async vs callbacks
Promise vs Async Await(2)
Comments
Confirm delete:
Do you really want to delete benchmark?