Improve performance in StandardComparisonStrategy#areEqual
All `instanceof` checks for array types are now performed only if `actual` is an array instance. This reduces the number of checks for all non-array instances, so `areEqual` will short-circuit faster.
@@ -83,8 +83,11 @@ public class StandardComparisonStrategy extends AbstractComparisonStrategy {
@Override
public boolean areEqual(Object actual, Object other) {
if (actual == null) return other == null;
- if (actual instanceof Object[] && other instanceof Object[])
- return java.util.Arrays.deepEquals((Object[]) actual, (Object[]) other);
+ Class<?> actualClass = actual.getClass();
+ if (actualClass.isArray()) {
+ Class<?> otherClass = other.getClass();
+ if (otherClass.isArray()) {
+ if (actualClass.getComponentType().isPrimitive() && otherClass.getComponentType().isPrimitive()) {
if (actual instanceof byte[] && other instanceof byte[])
return java.util.Arrays.equals((byte[]) actual, (byte[]) other);
if (actual instanceof short[] && other instanceof short[])
@@ -101,6 +104,12 @@ public class StandardComparisonStrategy extends AbstractComparisonStrategy {
return java.util.Arrays.equals((double[]) actual, (double[]) other);
if (actual instanceof boolean[] && other instanceof boolean[])
return java.util.Arrays.equals((boolean[]) actual, (boolean[]) other);
+ }
+
+ if (actual instanceof Object[] && other instanceof Object[])
+ return java.util.Arrays.deepEquals((Object[]) actual, (Object[]) other);
+ }
+ }
return actual.equals(other);
}
