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.`);
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.`);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
retry1 | |
retry2 |
Test name | Executions per second |
---|---|
retry1 | 25089.1 Ops/sec |
retry2 | 25973.5 Ops/sec |
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:
retry1
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:
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.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:
retry1
:retry2
: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:
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.