looping try-catch vs if

JavaScript performance comparison

Revision 3 of this test case created

Info

Testing try-catch vs if in a loop.

Preparation code

<script>

  function tryCatch(foo) {
    try {
      return foo.bar;
    }
    catch (ex) {
      return 0;
    }
  }

  function ifCheck(foo) {
    if (foo)
      return foo.bar;
    return 0;
  }

  function tryCatchLoop(foo) {
    for(var i=0; i<100; ++i)
      tryCatch(foo);
  }

  function ifCheckLoop(foo) {
    for(var i=0; i<100; ++i){
ifCheck(foo);
ifCheck(foo);
ifCheck(foo);
ifCheck(foo);
ifCheck(foo);
ifCheck(foo);
}
  }

</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
if
ifCheckLoop({
  bar: 1
});
pending…
try-catch
tryCatchLoop({
  bar: 1
});
pending…
try-catch failed
tryCatchLoop();
pending…
if failed
ifCheckLoop();
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