Anyone have any data to support a claim for which of the following two expressions is more efficient (meaning faster)? The use case is determining whether a List
is empty to inform an auxiliary method such as hasErrors()
.
Option 1: Use List.isEmpty()
public static Boolean hasErrors() {
// Assume getErrors() returns a List object
return !getErrors().isEmpty();
}
While semantically this approach makes a lot of sense, I wonder whether the !
operator has any material impact on the method's performance.
Option 2: Use List.size()
public static Boolean hasErrors() {
// Assume getErrors() returns a List object
return getErrors().size() > 0;
}
The questions raised by this alternative approach are: Is List.size()
inherently faster than List.isEmpty()
? And how does the performance of the greater than (>
) operator compare to that of the negation (!
) operator?