DEV Community

Discussion on: Early Returns in JavaScript

Collapse
 
1hko profile image
1hko • Edited

The benefit of "early return" or short-circuit evaluation is that you can skip the evaluation of an entire statement or expression. The skipped portion is performance gain.

Your benchmark isn't setup to show this though and a general comparison is not really possible. To see the benefit of this, you need to look at algorithms that have expensive computations where short-circuit evaluation can save a noticeable amount of space/time.

Imagine a path-finding algorithm, a chess AI, or other kind of solver where millions of computations are being made. In many cases, short-circuit evaluation can be leveraged to trim many possibilities or duplicates in the solution space. Simple algorithms can benefit too, like every and some. A simple if/else if/else doesn't allow one to form any meaningful opinion about the topic.