This rule flags when Array.findIndex() or Array.findLastIndex() is used with a simple equality comparison that could be replaced with the more direct indexOf() or lastIndexOf() methods.
The findIndex() and findLastIndex() methods are designed for complex search operations that require custom logic. When you only need to find the index of a specific value using strict equality comparison, these methods are unnecessarily complex.
Using indexOf() and lastIndexOf() for simple equality searches offers several advantages:
The findIndex() and findLastIndex() methods should be reserved for cases where you need custom comparison logic, use additional callback parameters (like index or array), or perform complex operations during the search.
Using findIndex() for simple equality searches creates unnecessary complexity and may impact performance. While the functional difference is minimal, the code becomes harder to read and maintain. In performance-critical applications, the overhead of callback function execution can accumulate over many operations.
Replace findIndex() with indexOf() when searching for a specific value using strict equality comparison.
const index = items.findIndex(x => x === 'target'); // Noncompliant
const index = items.indexOf('target');