Try/Catch Versus Massive IF

JavaScript performance comparison

Test case created by JC Fant

Info

Try Catch versus a large if statement.

Preparation code

<script>
  var d;
 
  var data = {};
 
  function isNotNull(data, property) {
   return data.hasOwnProperty(property) && typeof data[property] != "undefined" && data[property] != null;
  }
</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
Try/Catch
try {
 d = data.a.a;
} catch (e) {}
pending…
IF
if (data.hasOwnProperty("a") && typeof data.a != "undefined" && data.a != null && data.a.hasOwnProperty("a") && typeof data.a.a != "undefined" && data.a.a != null) {
 d = data.a.a;
}
pending…
IF notNull
if (isNotNull(data, "a") && isNotNull(data.a, "a")) {
 d = data.a.a;
}
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