ðBenchmarks
Measuring performance to find out how fast Results are
Throughout these guides, we have mentioned that throwing Java exceptions is slow. But... how slow? According to our benchmarks, throwing an exception is several orders of magnitude slower than returning a failed result.
This proves that using exceptional logic just to control normal program flow is a bad idea.
We should throw exceptions sparingly, even more so when developing performance-critical applications.
Benchmarking Result Library
This library comes with a set of benchmarks that compare performance when using results versus when using exceptions.
Simple Scenarios
The first scenarios compare the most basic usage: a method that returns a String
or fails, depending on a given int
parameter:
Using Exceptions
Using Results
Complex Scenarios
The next scenarios do something a little bit more elaborate: a method invokes the previous method to retrieve a String
; if successful, then converts it to upper case; otherwise transforms the "simple" error into a "complex" error.
Using exceptions
Using results
Conclusion
We provided insights into the Result library's performance through benchmarking. While our metrics corroborate that most codebases could benefit from using this library instead of throwing exceptions, its main goal is to help promote best practices and implement proper error handling.
To address performance concerns, benchmark your applications to gain reusable insights. These should guide your decisions on selecting frameworks and libraries.
Last updated