Synchronous try/catch vs. instanceof/return

JavaScript performance comparison

Test case created by Athan Clark

Info

This is a test between two common design patterns for synchronous code - either having the block of code throw an Error, or having the Error object be returned as a value and having it's type checked with instanceof. I am testing to see if throwing has as much overhead as value parsing, and if throwing is susceptible to bubbling.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function tryCatchRecurse(loops) {
      if (loops >= 0) {
        tryCatchRecurse(--loops);
      } else {
        throw new Error("Try / Catch Recursion Finished");
      }
    }
   
    function instanceofReturnRecurse(loops) {
      if (loops >= 0) {
        instanceofReturnRecurse(--loops);
      } else {
        return new Error("Instanceof / Return Recursion Finished");
      }
    }
   
    function valueEqualityReturnRecurse(loops) {
      if (loops >= 0) {
        valueEqualityReturnRecurse(--loops);
      } else {
        return 0;
      }
    }
   
    function tryCatchNamedOne() {
      throw new Error("Named Try / Catch Functions Finished");
    }
   
    function tryCatchNamedTwo() {
      tryCatchNamedOne();
    }
   
    function tryCatchNamedThree() {
      tryCatchNamedTwo();
    }
   
    function tryCatchNamedFour() {
      tryCatchNamedThree();
    }
   
    function tryCatchNamedFive() {
      tryCatchNamedFour();
    }
   
    function tryCatchNamedSix() {
      tryCatchNamedFive();
    }
   
    function tryCatchNamedSeven() {
      tryCatchNamedSix();
    }
   
    function tryCatchNamedEight() {
      tryCatchNamedSeven();
    }
   
    function tryCatchNamedNine() {
      tryCatchNamedEight();
    }
   
    function tryCatchNamedTen() {
      tryCatchNamedNine();
    }
   
   
    function instanceofReturnNamedOne() {
      return new Error("Named Try / Catch Functions Finished");
    }
   
    function instanceofReturnNamedTwo() {
      return instanceofReturnNamedOne();
    }
   
    function instanceofReturnNamedThree() {
      return instanceofReturnNamedTwo();
    }
   
    function instanceofReturnNamedFour() {
      return instanceofReturnNamedThree();
    }
   
    function instanceofReturnNamedFive() {
      return instanceofReturnNamedFour();
    }
   
    function instanceofReturnNamedSix() {
      return instanceofReturnNamedFive();
    }
   
    function instanceofReturnNamedSeven() {
      return instanceofReturnNamedSix();
    }
   
    function instanceofReturnNamedEight() {
      return instanceofReturnNamedSeven();
    }
   
    function instanceofReturnNamedNine() {
      return instanceofReturnNamedEight();
    }
   
    function instanceofReturnNamedTen() {
      return instanceofReturnNamedNine();
    }
   
   
    function valueEqualityReturnNamedOne() {
      return new Error("Named Try / Catch Functions Finished");
    }
   
    function valueEqualityReturnNamedTwo() {
      return valueEqualityReturnNamedOne();
    }
   
    function valueEqualityReturnNamedThree() {
      return valueEqualityReturnNamedTwo();
    }
   
    function valueEqualityReturnNamedFour() {
      return valueEqualityReturnNamedThree();
    }
   
    function valueEqualityReturnNamedFive() {
      return valueEqualityReturnNamedFour();
    }
   
    function valueEqualityReturnNamedSix() {
      return valueEqualityReturnNamedFive();
    }
   
    function valueEqualityReturnNamedSeven() {
      return valueEqualityReturnNamedSix();
    }
   
    function valueEqualityReturnNamedEight() {
      return valueEqualityReturnNamedSeven();
    }
   
    function valueEqualityReturnNamedNine() {
      return valueEqualityReturnNamedEight();
    }
   
    function valueEqualityReturnNamedTen() {
      return valueEqualityReturnNamedNine();
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Recursive Try/Catch Block 1
try {
  tryCatchRecurse(1);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 2
try {
  tryCatchRecurse(2);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 3
try {
  tryCatchRecurse(3);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 4
try {
  tryCatchRecurse(4);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 5
try {
  tryCatchRecurse(5);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 6
try {
  tryCatchRecurse(6);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 7
try {
  tryCatchRecurse(7);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 8
try {
  tryCatchRecurse(8);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 9
try {
  tryCatchRecurse(9);
} catch(e) {
  console.log(e);
}
pending…
Recursive Try/Catch Block 10
try {
  tryCatchRecurse(10);
} catch(e) {
  console.log(e);
}
pending…
Recursive Instanceof/Return Block 1
var err = instanceofReturnRecurse(1);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 2
var err = instanceofReturnRecurse(2);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 3
var err = instanceofReturnRecurse(3);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 4
var err = instanceofReturnRecurse(4);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 5
var err = instanceofReturnRecurse(5);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 6
var err = instanceofReturnRecurse(6);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 7
var err = instanceofReturnRecurse(7);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 8
var err = instanceofReturnRecurse(8);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 9
var err = instanceofReturnRecurse(9);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Instanceof/Return Block 10
var err = instanceofReturnRecurse(10);
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 1
var err = valueEqualityReturnRecurse(1);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 2
var err = valueEqualityReturnRecurse(2);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 3
var err = valueEqualityReturnRecurse(3);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 4
var err = valueEqualityReturnRecurse(4);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 5
var err = valueEqualityReturnRecurse(5);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 6
var err = valueEqualityReturnRecurse(6);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 7
var err = valueEqualityReturnRecurse(7);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 8
var err = valueEqualityReturnRecurse(8);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 9
var err = valueEqualityReturnRecurse(9);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Recursive Value Equality/Return Block 10
var err = valueEqualityReturnRecurse(10);
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Try/Catch Block 1
try {
  tryCatchNamedOne();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 2
try {
  tryCatchNamedTwo();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 3
try {
  tryCatchNamedThree();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 4
try {
  tryCatchNamedFour();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 5
try {
  tryCatchNamedFive();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 6
try {
  tryCatchNamedSix();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 7
try {
  tryCatchNamedSeven();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 8
try {
  tryCatchNamedEight();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 9
try {
  tryCatchNamedNine();
} catch(e) {
  console.log(e);
}
pending…
Named Function Try/Catch Block 10
try {
  tryCatchNamedTen();
} catch(e) {
  console.log(e);
}
pending…
Named Function Instanceof/Return Block 1
var err = instanceofReturnNamedOne();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 2
var err = instanceofReturnNamedTwo();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 3
var err = instanceofReturnNamedThree();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 4
var err = instanceofReturnNamedFour();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 5
var err = instanceofReturnNamedFive();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 6
var err = instanceofReturnNamedSix();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 7
var err = instanceofReturnNamedSeven();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 8
var err = instanceofReturnNamedEight();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 9
var err = instanceofReturnNamedNine();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Instanceof/Return Block 10
var err = instanceofReturnNamedTen();
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 1
var err = valueEqualityReturnNamedOne();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 2
var err = valueEqualityReturnNamedTwo();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 3
var err = valueEqualityReturnNamedThree();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 4
var err = valueEqualityReturnNamedFour();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 5
var err = valueEqualityReturnNamedFive();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 6
var err = valueEqualityReturnNamedSix();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 7
var err = valueEqualityReturnNamedSeven();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 8
var err = valueEqualityReturnNamedEight();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 9
var err = valueEqualityReturnNamedNine();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Named Function Value Equality/Return Block 10
var err = valueEqualityReturnNamedTen();
if (err === 0) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Try/Catch Block 1
try { (function() {
  throw new Error();
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 2
try { (function() {
  (function() {
    throw new Error();
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 3
try { (function() {
  (function() {
    (function() {
      throw new Error();
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 4
try { (function() {
  (function() {
    (function() {
      (function() {
        throw new Error();
      })()
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 5
try { (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          throw new Error();
        })()
      })()
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 6
try { (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            throw new Error();
          })()
        })()
      })()
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 7
try { (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              throw new Error();
            })()
          })()
        })()
      })()
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 8
try { (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              (function() {
                throw new Error();
              })()
            })()
          })()
        })()
      })()
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 9
try { (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              (function() {
                (function() {
                  throw new Error();
                })()
              })()
            })()
          })()
        })()
      })()
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Try/Catch Block 10
try { (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              (function() {
                (function() {
                  (function() {
                    throw new Error();
                  })()
                })()
              })()
            })()
          })()
        })()
      })()
    })()
  })()
})();
} catch(e) {
  console.log(e);
}
pending…
Anonymous Function Instanceof/Return Block 1
var err = (function() {
  return new Error();
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 2
var err = (function() {
  (function() {
    return new Error();
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 3
var err = (function() {
  (function() {
    (function() {
      return new Error();
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 4
var err = (function() {
  (function() {
    (function() {
      (function() {
        return new Error();
      })()
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 5
var err = (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          return new Error();
        })()
      })()
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 6
var err = (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            return new Error();
          })()
        })()
      })()
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 7
var err = (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              return new Error();
            })()
          })()
        })()
      })()
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 8
var err = (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              (function() {
                return new Error();
              })()
            })()
          })()
        })()
      })()
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 9
var err = (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              (function() {
                (function() {
                  return new Error();
                })()
              })()
            })()
          })()
        })()
      })()
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…
Anonymous Function Instanceof/Return Block 10
var err = (function() {
  (function() {
    (function() {
      (function() {
        (function() {
          (function() {
            (function() {
              (function() {
                (function() {
                  (function() {
                    return new Error();
                  })()
                })()
              })()
            })()
          })()
        })()
      })()
    })()
  })()
})()
if (err instanceof Error) {
  console.log(err);
} else {
  console.log("Something's fishy");
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment