ðBenchmarks
Measuring performance to find out how fast Results are
Last updated
Measuring performance to find out how fast Results are
Last updated
Copyright 2024 Guillermo Calvo
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.
This library comes with a set of benchmarks that compare performance when using results versus when using exceptions.
The first scenarios compare the most basic usage: a method that returns a String
or fails, depending on a given int
parameter:
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.
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.