For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/unicorn/prefer-set-size.md.
close

prefer-set-size

Added in v0.9.3

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-set-size': 'error',
    },
  },
]);

Prefer using Set#size instead of Array#length.

Converting a Set into an array solely to read its length performs unnecessary work. Use its size property directly.

Examples

// Incorrect
const uniqueCount = [...new Set(userIds)].length;

// Correct
const uniqueCount = new Set(userIds).size;
// Incorrect
const count = Array.from(items).length;

// Correct
const count = items.size;

Original Documentation