Skip to content

unicorn/prefer-array-find Perf ​

🚧 An auto-fix is still under development.

What it does ​

Encourages using Array.prototype.find instead of filter(...)[0] or similar patterns when only the first matching element is needed.

Why is this bad? ​

Using filter(...)[0] to get the first match is less efficient and more verbose than using find(...). find short-circuits when a match is found, whereas filter evaluates the entire array.

Examples ​

Examples of incorrect code for this rule:

js
const match = users.filter(u => u.id === id)[0];
const match = users.filter(fn).shift();

Examples of correct code for this rule:

js
const match = users.find(u => u.id === id);
const match = users.find(fn);

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-array-find
json
{
  "rules": {
    "unicorn/prefer-array-find": "error"
  }
}

References ​

Released under the MIT License.