While I was checking what’s new in Java 11, I saw that there are several new methods for String class. So I wanted to do a microbenchmark with old way of doing things and by using new methods. These new methods are;
boolean isBlank() String strip() Stream lines()
isBlank() is tested agains trim().isEmpty(), strip() is tested agains trim() and lines() is tested agains split().
Here are the results:
|trim + isEmpty||19854156|
Scores are based on operations per second so the more the better.
As you can see lines() is much faster than split().
strip() and trim() performed quite similar.
isBlank() outperformed trim() + empty().
You can check the benchmark code here.