Skip to content

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

Comments

In many cases, the non-exception path is as fast or faster than if/then/else. This is because compilers and interpreters tend to be optimized for non-exception paths.

If you actually throw and catch an exception, it might be slower but since most of the time there are no exceptions, the overall result is faster with exceptions. This is because there are no branches in control flow that would slow things down.

The micro benchmark in the original article is meaningless. This should be measured in a real-world situation where the catch is 5-10 levels up from the throw, and there are a lot more throws than catches.

AboutSource Built by g1lg1l

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