# prefer-array-search

Prefer direct Array search operations

Indirect searches allocate arrays, discard indices, or invoke callbacks for builtin equality.
Instead, you SHOULD use the direct Array search matching that result.

`find` and `findLast` return an element.
`some` and `includes` return membership.
`indexOf` and `lastIndexOf` return a position.
Reverse searches can change predicate or comparison order and require review.

- Category: performance
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
function firstPositive(values: int32[]): int32 | undefined {
    return values.filter((value) => value > 0).at(0);
}
```

## Accepted

```ds title="main.ds"
function firstPositive(values: int32[]): int32 | undefined {
    return values.find((value) => value > 0);
}
```

## Prior art

- [typescript-eslint · prefer-find](https://typescript-eslint.io/rules/prefer-find)
- [typescript-eslint · prefer-includes](https://typescript-eslint.io/rules/prefer-includes)
- [eslint-plugin-unicorn · prefer-array-index-of](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-array-index-of.md)
- [eslint-plugin-unicorn · prefer-array-some](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-array-some.md)

[language/linter/src/rules/performance/prefer_array_search.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/prefer_array_search.rs#L8)
