Skip to content

useSetSize (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useSetSize": "error"
}
}
}
}

Prefer reading the size property of a Set over converting the Set to an array to read its length.

A Set already keeps track of how many values it contains in its size property. Copying the values into a new array, with the spread syntax [...set] or with Array.from(set), only to read the length of that array, does extra work and creates an array that is thrown away right after.

The rule reports [...set].length and Array.from(set).length when it can tell that set is a Set. This is the case when the value is:

  • created with new Set();
  • a const variable initialized with new Set();
  • a variable or a function parameter with the type Set or ReadonlySet;
  • marked as a Set or a ReadonlySet with as or satisfies.
function isUnique(array) {
return [...new Set(array)].length === array.length;
}
code-block.js:2:12 lint/nursery/useSetSize  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This code copies a Set into an array only to count its values.

1 │ function isUnique(array) {
> 2 │ return [...new Set(array)].length === array.length;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

ℹ Copying every value takes extra time and memory, while the size property of the Set already holds the count.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use size instead.

1 1 │ function isUnique(array) {
2 │ - ····return·[...new·Set(array)].length·===·array.length;
2 │ + ····return·new·Set(array).size·===·array.length;
3 3 │ }
4 4 │

const items = new Set([1, 2, 3, 4, 5]);
if ([...items].length > 3) {}
code-block.js:2:5 lint/nursery/useSetSize  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This code copies a Set into an array only to count its values.

1 │ const items = new Set([1, 2, 3, 4, 5]);
> 2 │ if ([...items].length > 3) {}
│ ^^^^^^^^^^^^^^^^^
3 │

ℹ Copying every value takes extra time and memory, while the size property of the Set already holds the count.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use size instead.

1 1 │ const items = new Set([1, 2, 3, 4, 5]);
2 │ - if·([...items].length·>·3)·{}
2 │ + if·(items.size·>·3)·{}
3 3 │

const uniqueCount = Array.from(new Set(userIds)).length;
code-block.js:1:21 lint/nursery/useSetSize  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This code copies a Set into an array only to count its values.

> 1 │ const uniqueCount = Array.from(new Set(userIds)).length;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Copying every value takes extra time and memory, while the size property of the Set already holds the count.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use size instead.

1 │ - const·uniqueCount·=·Array.from(new·Set(userIds)).length;
1 │ + const·uniqueCount·=·new·Set(userIds).size;
2 2 │

function countTags(tags: Set<string>) {
return [...tags].length;
}
code-block.ts:2:12 lint/nursery/useSetSize  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This code copies a Set into an array only to count its values.

1 │ function countTags(tags: Set<string>) {
> 2 │ return [...tags].length;
│ ^^^^^^^^^^^^^^^^
3 │ }
4 │

ℹ Copying every value takes extra time and memory, while the size property of the Set already holds the count.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use size instead.

1 1 │ function countTags(tags: Set<string>) {
2 │ - ····return·[...tags].length;
2 │ + ····return·tags.size;
3 3 │ }
4 4 │

function isUnique(array) {
return new Set(array).size === array.length;
}
const items = new Set([1, 2, 3, 4, 5]);
if (items.size > 3) {}
// The rule can't tell whether `values` is a `Set`.
const count = [...values].length;
// The array contains more than the values of the `Set`.
const total = [...new Set(array), extra].length;