Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Multipromise resulter
(version: 0)
Comparing performance of:
Function based vs Array based
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function delay(ms, val = true) { let timeoutId; return new Promise(resolve => { timeoutId = setTimeout(() => resolve(val), ms); }); } function runTest(resultFunc) { const res = resultFunc(); for(let i=0; i<20; i++) { res.add(delay(i)); } return res.get(); }
Tests:
Function based
const createResultPromiseFunc = () => { let res = Promise.resolve(); return { get() { return res; }, add(nextPromise) { res = res.then(() => nextPromise); return this; } }; }; runTest(createResultPromiseFunc);
Array based
const createResultPromiseArray = () => { const res = []; return { get() { return Promise.all(res); }, add(nextPromise) { res.push(nextPromise); return this; } }; }; runTest(createResultPromiseArray);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Function based
Array based
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Browser/OS:
Chrome 108 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Function based
857.7 Ops/sec
Array based
844.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmarking data and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Definition JSON** The benchmark definition represents two different ways to implement a "Multipromise Resulter" in JavaScript: 1. **Function-based implementation**: The first benchmark definition uses a function `createResultPromiseFunc` that returns an object with two methods: `get()` and `add(nextPromise)`. The `get()` method returns the resolved promise, while the `add(nextPromise)` method adds the next promise to the chain. 2. **Array-based implementation**: The second benchmark definition uses a function `createResultPromiseArray` that returns an object with two methods: `get()` and `add(nextPromise)`. However, instead of using a promise, it returns an array that is populated with promises through the `add()` method. The `get()` method then resolves to the all-promise resolved in the array. **Options Compared** The benchmark compares the performance of these two different approaches: 1. **Function-based implementation**: This approach uses a promise chain to handle asynchronous operations. 2. **Array-based implementation**: This approach uses an array to store promises and chains them together using the `add()` method. **Pros and Cons of Each Approach** **Function-Based Implementation:** Pros: * Easier to understand and implement for developers familiar with promise chaining. * Less memory-intensive, as only one promise object is created. Cons: * May have slower performance due to the overhead of creating and managing a promise chain. **Array-Based Implementation:** Pros: * Can be more efficient in terms of memory usage, as it uses an array instead of a promise chain. * Allows for easier handling of asynchronous operations with multiple promises using array methods like `push()`. Cons: * More complex to understand and implement, especially for developers without experience with arrays and promise chains. * May have slower performance due to the overhead of creating and managing the array and its promises. **Library Used (if applicable)** In both benchmark definitions, no external libraries are used. The implementations rely solely on JavaScript's built-in features, such as `Promise` objects and array methods. **Special JS Features or Syntax** No special JavaScript features or syntax are used in these benchmark definitions. They rely only on standard JavaScript constructs. **Other Alternatives** Other alternatives to implement a "Multipromise Resulter" could include: * Using a task queue like `async/await` with `Promise.all()` or `Promise.race()` * Implementing a custom data structure, such as a linked list or a stack, to handle asynchronous operations * Utilizing a third-party library that provides similar functionality Keep in mind that each of these alternatives would have their own pros and cons, and the choice ultimately depends on the specific requirements and constraints of the project.
Related benchmarks:
await delay vs setTimeout
await delay vs setTimeout
async for vs promise all (setTimeout version)
needless async impact
Comments
Confirm delete:
Do you really want to delete benchmark?