Tests:
  • retry1

    x
     
    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>

Previous results

Experimental features:

  • Test case name Result
    retry1
    retry2

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36
Chrome 90 on Windows
View result in a separate tab
Test name Executions per second
retry1 25089.1 Ops/sec
retry2 25973.5 Ops/sec