# prefer-length-over-count

Prefer stored array length over counting an unchanged iterator

Counting every value from an unchanged array iterator performs linear work to recover the array's stored length.
Instead, you SHOULD read `length` from the array directly.

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

## Reported

```ds title="main.ds"
function length(values: int32[]): isize {
    return values.iterator().count();
}
```

## Accepted

```ds title="main.ds"
function length(values: int32[]): isize {
    return values.length;
}
```

## Prior art

- [Clippy · iter_count](https://rust-lang.github.io/rust-clippy/master/index.html#iter_count)

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