# unicorn/prefer-set-size

> Require `Set#size` instead of converting the `Set` to an array to read `length`

`[...set].length` copies every item in the `Set` into a new array, reads one number and throws the array away. A `Set` already knows how many items it has, and `set.size` tells you.

The rule reports `[...set].length` and `Array.from(set).length` when `set` is a `new Set()` or a `const` that's initialized with one.

`--fix` rewrites it to `.size`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/prefer-set-size" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```js
const uniqueCount = [...new Set(tags)].length;

const visitors = new Set(ids);
console.log(Array.from(visitors).length);
```

Not reported:

{/* good */}
```js
const uniqueCount = new Set(tags).size;

const visitors = new Set(ids);
console.log(visitors.size);
```

If the rule can't tell that the value is a `Set`, it doesn't report anything:

{/* good */}
```js
function count(values) {
  return [...values].length;
}
```
