Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
retry bench
(version: 0)
Comparing performance of:
retry1 vs retry2
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
retry1
function callMe(success, failure) { const chance = 0; setTimeout(() => { if (chance >= Math.random()) { success("Succeeded"); } else { failure(new Error("Failure")); } }, 0); } const retry1 = async (func, attempts) => { while (true) { try { return await new Promise(func); } catch (err) { attempts--; if (attempts === 0) throw err; } } }; const t0 = performance.now(); retry1(callMe, 50).then(res => console.log("success: " + res)).catch(res => console.log("failure: " + res)); const t1 = performance.now(); console.log(`Call to retry1 took ${t1 - t0} milliseconds.`);
retry2
function callMe(success, failure) { const chance = 0; setTimeout(() => { if (chance >= Math.random()) { success("Succeeded"); } else { failure(new Error("Failure")); } }, 0); } const retry2 = (func, attempts) => new Promise(func).catch((err) => { if (attempts > 0) { return retry2(func, attempts - 1); } throw err; }); const t0 = performance.now(); retry2(callMe, 50).then(res => console.log("success: " + res)).catch(res => console.log("failure: " + res)); const t1 = performance.now(); console.log(`Call to retry2 took ${t1 - t0} milliseconds.`);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
retry1
retry2
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):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The provided JSON represents the benchmark definition for a simple retry mechanism in JavaScript. The script preparation code and HTML preparation code are empty, which means that no custom setup or rendering is required for this benchmark. **Test Cases** There are two test cases: 1. `retry1` 2. `retry2` Both test cases measure the performance of a retry mechanism in JavaScript. They test how many times they can execute a function before it fails and how long each execution takes. **Benchmarking Options** The two test cases use different approaches to implement the retry mechanism: 1. **`retry1`**: This test case uses an `async/await` syntax to call the `callMe` function, which simulates a random failure with a 50% chance. The `retry1` function is then called repeatedly until it succeeds or runs out of attempts (50 in this case). The benchmark measures the time taken for each retry attempt. 2. **`retry2`**: This test case uses a recursive approach to implement the retry mechanism. It creates a new promise that calls itself recursively with decreasing attempts until the promise fails or succeeds. **Pros and Cons** Here are some pros and cons of each approach: 1. **`retry1`**: * Pros: + Easy to understand and maintain + Uses built-in async/await syntax, which is familiar to many developers * Cons: + Can be slower due to the overhead of creating and resolving promises 2. **`retry2`**: * Pros: + Can be faster due to the elimination of promise creation and resolution overhead * Cons: + More complex to understand and maintain, especially for developers without prior experience with recursive promises **Libraries and Special Features** There are no notable libraries used in these test cases. However, it's worth noting that both test cases use JavaScript's built-in `setTimeout` function to introduce randomness into the simulation. **Other Considerations** When interpreting benchmark results, consider the following: * **Device and browser variability**: The provided benchmark result shows execution counts per second for two different devices (Desktop) with Chrome 90. This highlights the importance of considering device and browser variations when comparing performance metrics. * **Execution count vs. time**: Execution counts per second are an effective way to compare performance, but they might not accurately represent real-world scenarios where code execution is more complex. **Alternatives** If you're interested in exploring alternative approaches or libraries for retry mechanisms, consider the following: * **`retry-obj` library**: This JavaScript library provides a simple and efficient way to implement retry logic. It's designed to be fast and flexible. * **`Axios` library with retry mechanism**: If you're working with HTTP requests, consider using the `Axios` library, which includes built-in support for retries.
Related benchmarks:
repeat
Promise vs Async
Promise vs Async 2
Try/catch vs .catch
Comments
Confirm delete:
Do you really want to delete benchmark?