Skip to content

Comment on JS performance: try / catch versus checking for undefined parent

Comments

Look at the latest revision: http://jsperf.com/try-catch-error-perf/16

Your "not surprising" result is only valid in Chrome and Opera. Other browsers are faster at try/catch.

The latest revision is a completely different test. As klodolph explains in another comment, the presumably common case of no exception is generally how try/catch is optimized. So it stands to reason that the exception case would be slower and that there is potential for the non-exceptional case to be faster.

That test is broken. In JavaScript the value of an undefined property will be `undefined`, not `null` so the second test is doing twice the work of the first.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.