Editing Synchronous try/catch vs. instanceof/return This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) 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. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) 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(); } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(1); } catch(e) { console.log(e); } Test 2 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(2); } catch(e) { console.log(e); } Test 3 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(3); } catch(e) { console.log(e); } Test 4 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(4); } catch(e) { console.log(e); } Test 5 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(5); } catch(e) { console.log(e); } Test 6 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(6); } catch(e) { console.log(e); } Test 7 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(7); } catch(e) { console.log(e); } Test 8 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(8); } catch(e) { console.log(e); } Test 9 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(9); } catch(e) { console.log(e); } Test 10 Title Async (check if this is an asynchronous test) Code try { tryCatchRecurse(10); } catch(e) { console.log(e); } Test 11 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(1); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 12 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(2); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 13 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(3); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 14 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(4); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 15 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(5); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 16 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(6); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 17 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(7); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 18 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(8); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 19 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(9); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 20 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnRecurse(10); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 21 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(1); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 22 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(2); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 23 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(3); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 24 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(4); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 25 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(5); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 26 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(6); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 27 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(7); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 28 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(8); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 29 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(9); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 30 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnRecurse(10); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 31 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedOne(); } catch(e) { console.log(e); } Test 32 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedTwo(); } catch(e) { console.log(e); } Test 33 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedThree(); } catch(e) { console.log(e); } Test 34 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedFour(); } catch(e) { console.log(e); } Test 35 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedFive(); } catch(e) { console.log(e); } Test 36 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedSix(); } catch(e) { console.log(e); } Test 37 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedSeven(); } catch(e) { console.log(e); } Test 38 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedEight(); } catch(e) { console.log(e); } Test 39 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedNine(); } catch(e) { console.log(e); } Test 40 Title Async (check if this is an asynchronous test) Code try { tryCatchNamedTen(); } catch(e) { console.log(e); } Test 41 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedOne(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 42 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedTwo(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 43 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedThree(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 44 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedFour(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 45 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedFive(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 46 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedSix(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 47 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedSeven(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 48 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedEight(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 49 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedNine(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 50 Title Async (check if this is an asynchronous test) Code var err = instanceofReturnNamedTen(); if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 51 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedOne(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 52 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedTwo(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 53 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedThree(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 54 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedFour(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 55 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedFive(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 56 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedSix(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 57 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedSeven(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 58 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedEight(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 59 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedNine(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 60 Title Async (check if this is an asynchronous test) Code var err = valueEqualityReturnNamedTen(); if (err === 0) { console.log(err); } else { console.log("Something's fishy"); } Test 61 Title Async (check if this is an asynchronous test) Code try { (function() { throw new Error(); })(); } catch(e) { console.log(e); } Test 62 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { throw new Error(); })() })(); } catch(e) { console.log(e); } Test 63 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { throw new Error(); })() })() })(); } catch(e) { console.log(e); } Test 64 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { (function() { throw new Error(); })() })() })() })(); } catch(e) { console.log(e); } Test 65 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { (function() { (function() { throw new Error(); })() })() })() })() })(); } catch(e) { console.log(e); } Test 66 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { (function() { (function() { (function() { throw new Error(); })() })() })() })() })() })(); } catch(e) { console.log(e); } Test 67 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { (function() { (function() { (function() { (function() { throw new Error(); })() })() })() })() })() })() })(); } catch(e) { console.log(e); } Test 68 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { (function() { (function() { (function() { (function() { (function() { throw new Error(); })() })() })() })() })() })() })() })(); } catch(e) { console.log(e); } Test 69 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { (function() { (function() { (function() { (function() { (function() { (function() { throw new Error(); })() })() })() })() })() })() })() })() })(); } catch(e) { console.log(e); } Test 70 Title Async (check if this is an asynchronous test) Code try { (function() { (function() { (function() { (function() { (function() { (function() { (function() { (function() { (function() { (function() { throw new Error(); })() })() })() })() })() })() })() })() })() })(); } catch(e) { console.log(e); } Test 71 Title Async (check if this is an asynchronous test) Code var err = (function() { return new Error(); })() if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 72 Title Async (check if this is an asynchronous test) Code var err = (function() { (function() { return new Error(); })() })() if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 73 Title Async (check if this is an asynchronous test) Code var err = (function() { (function() { (function() { return new Error(); })() })() })() if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 74 Title Async (check if this is an asynchronous test) Code var err = (function() { (function() { (function() { (function() { return new Error(); })() })() })() })() if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 75 Title Async (check if this is an asynchronous test) Code var err = (function() { (function() { (function() { (function() { (function() { return new Error(); })() })() })() })() })() if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 76 Title Async (check if this is an asynchronous test) Code var err = (function() { (function() { (function() { (function() { (function() { (function() { return new Error(); })() })() })() })() })() })() if (err instanceof Error) { console.log(err); } else { console.log("Something's fishy"); } Test 77 Title Async (check if this is an asynchronous test) Code 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"); } Test 78 Title Async (check if this is an asynchronous test) Code 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"); } Test 79 Title Async (check if this is an asynchronous test) Code 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"); } Test 80 Title Async (check if this is an asynchronous test) Code 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"); }